I'm using carrierwave and I have this problem: Suppose once the project has been delivered you need to add a section where the images in the system need to be displayed with a different size. I don' t want to regenerate the new dimension for each one of the images already in the system. I want to be able to generate (and cache it) whenever a view demands. Something like: " /> . If the new size 500x150 already exists, then returns the cached url, else generate it and return the cached url
I like pretty much Carrierwave but unfortunately doesn't have any on the fly resize feature out of the box. Everyone says it should be pretty simple add this feature but I found almost nothing. The only thing which goes pretty close is this uploader https://gist.github.com/DAddYE/1541912 I had to modify it to make it work so here is my version
class ImageUploader < FileUploader
include CarrierWave::RMagick
#version :thumb do
# process :resize_to_fill => [100,100]
#end
#
#version :thumb_square do
# process :resize_to_fill => [100,100]
#end
#
#version :full do
# process :resize_to_fit => [550, 550]
#end
def re_size(string_size)
if self.file.nil?
return self
end
begun_at = Time.now
string_size.gsub!(/#/, '!')
uploader = Class.new(self.class)
uploader.versions.clear
uploader.version_names = [string_size]
img = uploader.new(model, mounted_as)
img.retrieve_from_store!(self.file.identifier)
cached = File.join(CarrierWave.root, img.url)
unless File.exist?(cached)
img.cache!(self)
img.send(:original_filename=, self.file.original_filename)
size = string_size.split(/x|!/).map(&:to_i)
resizer = case string_size
when /[!]/ then :resize_to_fit
# add more like when />/ then ...
else :resize_to_fill
end
img.send(resizer, *size)
FileUtils.mv(img.file.file, cached)
#img.store!
end
img
end
def extension_white_list
%w[jpg jpeg gif png]
end
def filename
Digest::MD5.hexdigest(original_filename) << File.extname(original_filename) if original_filename
end
def cache_dir
"#{Rails.root}/tmp/uploads"
end
def default_url
'/general/no-image.png'
end
end
The problem with this version is that apparently when calling re_size("100x100").url, the url gets generated and returned before the actual resized image is created resulting in a page with broken links which displays good on any subsequent refresh.
Anyone achieved better results willing to share? :)
Please don't tell me to switch to Dragonfly. I'm using Carrierwave and i really like it. Also it seamlessly integrates with RailsAdmin which is part of my projects too.