Caching of image dimensions

682 views
Skip to first unread message

Luis Lavena

unread,
Feb 24, 2010, 6:44:57 AM2/24/10
to carrierwave
Hello,

I've been looking into CarrierWave to replace our current approach to
file attachments, and found it good. However, I couldn't find a way to
store more information about the attachment than just the filename.

Let me explain a bit further.

Right now we use Paperclip and extended it to cache in the database
the image dimensions (w x h) so when rendered by Rails image_tag, the
HTML generated contains the size information and avoids displacement
of elements while images loads.

We also compute the size for the different versions, but using
ImageScience geometry computation to do it real-time.

Pardon my ignorance, but couldn't find an example or in the code how
to achieve this.

This has been implemented? and if not, from 1 to 10, how complicated
will be to extend CarrierWave to do it outside the official gem?
Asking this because the changes in Paperclip require monkeypatching...

Thank you,
--
Luis Lavena

Jonas Nicklas

unread,
Feb 24, 2010, 5:37:03 PM2/24/10
to carri...@googlegroups.com
There is currently no support for automatically assigning columns in
Carrierwave, but it's very easy to do it yourself. You can use a
callback in the uploader or do it with a before_validation filter. If
you want to extend Carrierwave to do this, it shouldn't be hard,
Carierwave follows the same modular principle as Rails 3 does.

Jonas

larspind

unread,
Apr 6, 2010, 9:38:19 AM4/6/10
to carrierwave
I've implemented this for one version, but it wasn't so easy as I'd
originally thought.

The tricky part was getting it to work across form submission errors,
too. Specifically, before :store callbacks seemed to get called
several times with different params and values for @file during the
course of saving the object.

Here's the code that worked for me in the end. It can probably be
improved upon.

Here's my entire uploader class, but the important parts are the
capture_size methods.

Enjoy!

//Lars

--

class HeaderUploader < CarrierWave::Uploader::Base
storage :right_s3

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

def url
["http://#{s3_bucket}.s3.amazonaws.com/", path].compact.join
end

before :cache, :capture_size_before_cache
before :retrieve_from_cache, :capture_size_after_retrieve_from_cache

def capture_size_before_cache(new_file)
model.header_width, model.header_height = `identify -format "%wx
%h" #{new_file.path}`.split(/x/)
end

def capture_size_after_retrieve_from_cache(cache_name)
model.header_width, model.header_height = `identify -format "%wx
%h" #{@file.path}`.split(/x/)
end

def dimensions
"#{model.header_width}x#{model.header_height}"
end
end

Nick Hoffman

unread,
Sep 13, 2011, 2:39:52 PM9/13/11
to carri...@googlegroups.com
Thanks for posting your solution. I strongly recommend you use Escape.shell_command, or Shellwords#shellescape to ensure that malicious command injections don't take place.
Reply all
Reply to author
Forward
0 new messages