class ImageUploader < ApplicationUploader
include CarrierWave::MiniMagick
version :medium do
process resize_to_fill: [1200, 835, 'North']
end
version :small do
process resize_to_fill: [610, 421, 'North']
end
version :directory_image do
process resize_to_fill: [365, 421, 'North']
end
version :directory_banner do
process resize_to_fill: [454, 320, 'North']
process :convert_to_grayscale
end
version :thumb do
process resize_to_fill: [600, 600, 'North']
end
def pre_limit(file)
# if file && (file.size < 1.megabytes || file.size > 10.megabytes)
# raise Exception.new("File size must be between 1MB and 10MB")
# end
true
end
private
def convert_to_grayscale
manipulate! do |img|
img.colorspace("Gray")
img.brightness_contrast("10x0")
img
end
end
end
Above is the code for the image uploader. It takes too long to create all the versions because of which the user experience is getting affected. The response takes about 7 seconds if I upload the image with these versions and it takes 2.13 seconds if remove all these versions. Is there a way to reduce the image upload time by keeping all these versions?
Related
I am trying make a screenshot functionality of the actual state of the Window/Screen. How can I achieve this using Ruby Gosu?
Use Gosu's Gosu.render method
require "gosu"
class Window < Gosu::Window
def initialize(*args)
super
end
def draw
Gosu.draw_rect(100, 100, 100, 100, Gosu::Color::YELLOW)
end
def button_down(id)
render("screenshot.png") if id == Gosu::KB_F12
end
def render(filename)
Gosu.render(600, 600, retro: false) do
# Put drawing code here, i.e.
draw
end.save(filename)
end
end
Window.new(600, 600, false).show
You also can use Gosu.render without a window if that is desired:
require "gosu"
image = Gosu.render(600, 600, retro: false) do
Gosu.draw_rect(100, 100, 100, 100, Gosu::Color::YELLOW)
end
image.save("image.png")
I`m use Rails 3.2.11+Carrierwave.
help please, how to set the default thumb for a specific content type, such as .doc or .docx?
Default thumb placed in /public/assets - thumb_for_doc.jpg
my uploader:
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
#include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
storage :file
# storage :fog
def store_dir
"uploads/order_#{model.order.id}" unless model.order.nil?
end
process :set_content_type
version :pdf, :if => :pdf? do
process :cover
process :resize_to_fit => [100, 100]
process :convert => :jpg
def full_filename (for_file = model.source.file)
super.chomp(File.extname(super)) + '.jpg'
end
end
version :msword, :if => :msword? do
#????
end
version :thumb, :if => :image? do
process :resize_to_limit => [100, 100]
end
def filename
if model.document_specification_id
document_specification = Lists::DocumentSpecification.find_by_id(model.document_specification_id)
margins = Lists::PrintMargin.find_by_id(document_specification.print_margin_id).margin
paper_type = Lists::PaperType.find_by_id(Lists::PaperSpecification.find_by_id(document_specification.paper_specification_id).paper_type_id).paper_type
paper_size = Lists::PaperSize.find_by_id(Lists::PaperSpecification.find_by_id(document_specification.paper_specification_id).paper_size_id).size
"#{secure_token}_PP_#{paper_size.parameterize}_#{paper_type.parameterize}_#{model.quantity}_#{margins.parameterize}.#{file.extension}" if original_filename.present?
else
"#{secure_token}_PP_not_set_spesification.#{file.extension}" if original_filename.present?
end
end
def cover
manipulate! do |frame, index|
frame if index.zero? # take only the first page of the file
end
end
protected
def secure_token
var = :"##{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.hex(4))
end
def image?(file)
file.content_type.start_with? 'image'
end
def pdf?(file)
file.content_type.start_with? 'application/pdf'
end
def msword?(file)
file.content_type.start_with? 'application/msword'
end
end
thanks!
I am writing an automated testing program which will test some web programs that are sometimes slow to load certain AJAX calls. For instance the user will click 'Query' which will result in a HTML 'loading' overlay for anywhere from 15 to 90 seconds. When the search completes, it will then update a table on the same page with the results.
So obviously I can increase the waiting time individually like so:
browser.td(:id => 'someId').when_present.some_action #=> will wait 30 seconds
browser.td(:id => 'someId').when_present(90).some_action #=> will wait *90* seconds
But is there a way to modify (in my case increase) the time so Watir-Webdriver always waits 90 seconds on .when_present like so:
browser.some_default = 90
browser.td(:id => 'someId').when_present.some_action #=> will wait *90* seconds
A few words of warning: Client timeout will not affect when_present. Nor will implicit wait.
Update: This monkey patch has been merged into watir-webdriver and so will no longer be needed in watir-webdriver v0.6.5. You will be able to set the timeout using:
Watir.default_timeout = 90
The wait methods are defined similar to:
def when_present(timeout = 30)
message = "waiting for #{selector_string} to become present"
if block_given?
Watir::Wait.until(timeout, message) { present? }
yield self
else
WhenPresentDecorator.new(self, timeout, message)
end
end
As you can see, the default timeout of 30 seconds is hard-coded. Therefore, there is no easy way to change it everywhere.
However, you could monkey patch the wait methods to use a default time and set it to what you want. The following monkey patch will set the default timeout to 90 seconds.
require 'watir-webdriver'
module Watir
# Can be changed within a script with Watir.default_wait_time = 30
#default_wait_time = 90
class << self
attr_accessor :default_wait_time
end
module Wait
class << self
alias old_until until
def until(timeout = Watir.default_wait_time, message = nil, &block)
old_until(timeout, message, &block)
end
alias old_while while
def while(timeout = Watir.default_wait_time, message = nil, &block)
old_while(timeout, message, &block)
end
end # self
end # Wait
module EventuallyPresent
alias old_when_present when_present
def when_present(timeout = Watir.default_wait_time, &block)
old_when_present(timeout, &block)
end
alias old_wait_until_present wait_until_present
def wait_until_present(timeout = Watir.default_wait_time)
old_wait_until_present(timeout)
end
alias old_wait_while_present wait_while_present
def wait_while_present(timeout = Watir.default_wait_time)
old_wait_while_present(timeout)
end
end # EventuallyPresent
end # Watir
Include the patch after the watir webdriver code is loaded.
The idea is, I want to convert a PDF to an image per page. I've tried other methods which are on Stackoverflow but to no avail. Below is my uploader file.
Uploader
class PdfUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
#//TODO - Remove converted images when deleting an entry, maybe? if needed?
storage :file
#//TODO - Add images into the same folder. model.id can't be accessed in a custom process.
def store_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def cache_dir
"#{Rails.root}/public/uploads/#{model.class.to_s.underscore}/#{mounted_as}/tmp"
end
def extension_white_list
%w(pdf)
end
def filename
super.chomp(File.extname(super)) + '.png'
end
version :pages do
process :create_pages
process convert: 'png'
end
process convert: 'png'
version :thumb do
process :cover
process :resize_to_fill => [200, 200, Magick::CenterGravity]
process convert: 'png'
end
def cover
manipulate! do |frame, index|
frame if index.zero?
end
end
def create_pages
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
end
The PDF will get converted to images for each page. But it will not recognize the model.id and store it in that folder. Instead it will store in the directory above that.
I've tried the adding the following to force the making of the folder, but it doesn't seem to know the model.id through a little bit of testing?
Tried to Make Dir
def create_pages
Dir.mkdir(store_dir) unless File.exists?(store_dir)
manipulate! do |frame, index|
frame.write("#{store_dir}/#{index}.png")
end
end
Help would be much appreciated, I've been stuck on this for a while and had to leave it for a couple of days out of frustration.
Thank you in advance.
This is most likely because you are saving your images with frame.write before the model itself is saved, and therefore also before an ID has been assigned.
Have a look at this answer for a potential solution: Carrierwave : error with model.id in the store_path
I am migrating from Paperclip to Carrierwave. Here I am trying to convert the processing commands for thumbnail generations:
has_attached_file :image,
styles: {
thumb: '220x140#',
big: '620x600>',
no_proportion: '255x162!'
},
convert_options: {
all: '-strip',
thumb: '-delete 1--1',
no_proportion: '-delete 1--1'
}
I am planning to use MiniMagick. I got that I convert from 220x140# to resize_to_fill(220,140), but I am not sure how to convert all the other commands.
P.S. It would be better if I can reuse the existing ImageMagick commands and parameters, even for resizing (i.e. not using built-in helper resizer).
I have done the following. However I am not sure if it is totally equivalent.
process :strip
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [220,140]
end
version :mobile do
process :resize_to_fill => [320,210]
end
version :mobile_small do
process :resize_to_fill => [256,168]
end
version :big do
process :resize_to_limit => [620,600]
end
version :groupbuy_ad do
process :resize_to_fill => [96,60]
end
version :email do
process :resize_to_fill => [125,125]
end
version :widget_165 do
process :resize_to_fill => [165,105]
end
version :widget_100 do
process :resize_to_fill => [100,64]
end
version :no_proportion do
process :resize => '255x162!'
end
def strip
manipulate! do |img|
img.strip
img = yield(img) if block_given?
img
end
end
def resize(dimension)
manipulate! do |img|
img.resize dimension
img = yield(img) if block_given?
img
end
end
def get_first_frame
manipulate! do |img|
img = img.delete '1--1'
end
end