Skip to content

Instantly share code, notes, and snippets.

@DAddYE
Created December 30, 2011 22:57
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save DAddYE/1541912 to your computer and use it in GitHub Desktop.
Save DAddYE/1541912 to your computer and use it in GitHub Desktop.
CarrierWave on the fly resizer (work as dragonfly)
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def thumb(size)
begun_at = Time.now
size.gsub!(/#/, '!')
uploader = Class.new(self.class)
uploader.versions.clear
uploader.version_names = [size]
img = uploader.new
img.retrieve_from_store!(identifier)
cached = File.join(CarrierWave.root, img.url)
unless File.exist?(cached)
img.cache!(self)
img.send(:original_filename=, original_filename)
size = size.split('x').map(&:to_i)
resizer = case size
when /[!#]/ then :resize_to_fit
# add more like when />/ then ...
else :resize_to_fill
end
img.send(resizer, *size)
img.store!
logger.debug 'RESIZE', begun_at, img.store_path
end
img
end
def store_dir
'uploads'
end
def cache_dir
Padrino.root('tmp/uploads')
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 default_url
'/images/no-image.png'
end
end
>> i = ImageUploader.new
=> #<ImageUploader:0x1044314f8 @model=nil, @mounted_as=nil>
>> i.download!('http://a1.twimg.com/profile_images/682283839/PadrinoLogo_reasonably_small.jpg')
=> [:process!, :assign_parent_cache_id, :cache_versions!]
>> i.store!
=> [:store_versions!]
>> i.url
=> "/uploads/35c40bbe3a9170e2901165990c34a173.jpg"
>> i.thumb('100x100').url
DEBUG - RESIZE (0.1124ms) uploads/100x100_35c40bbe3a9170e2901165990c34a173.jpg
=> "/uploads/100x100_35c40bbe3a9170e2901165990c34a173.jpg"
>> i.thumb('100x100').url
=> "/uploads/100x100_35c40bbe3a9170e2901165990c34a173.jpg"
@skop
Copy link

skop commented Jan 17, 2013

I'm trying to use this code in a rails project, but with the default store_dir:

"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"

I get the error: "Called id for nil, which would mistakenly be 4", which I believe is because the current object's model isn't being passed into the thumb method (or it's being reset). This is then causing the model.id in store_dir to fail because model is nil and identifier is also nil. If I just use the static "uploads" directory as you do, the resized images do not have any extension or the original filename. It is just "uploads/100x100". I am calling this from a view like so:

member.image.resize('100x100').url

Member is the model and image is the column name. Any ideas of what I'm doing wrong?

@skop
Copy link

skop commented Jan 18, 2013

Also, on line 17, you overwrite size, stripping out any !# that would trigger the different manipulation types in the case statement (regex compare) just below.

size = size.split('x').map(&:to_i)
resizer = case size
when /[!#]/ then :resize_to_fit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment