Rails - Paperclip, getting width and height of image in model

Posted by Corey Tenold on Stack Overflow See other posts from Stack Overflow or by Corey Tenold
Published on 2010-05-04T19:52:55Z Indexed on 2010/05/04 19:58 UTC
Read the original article Hit count: 190

Filed under:
|

Trying to get the width and height of the uploaded image while still in the model on the initial save.

Any way to do this?

Here's the snippet of code I've been testing with from my model. Of course it fails on "instance.photo_width".

has_attached_file :photo,
                      :styles => {
                      :original  => "634x471>",
                      :thumb => Proc.new { |instance|                       
                                  ratio = instance.photo_width/instance.photo_height
                                  min_width   = 142
                                  min_height  = 119
                                  if ratio > 1
                                    final_height  = min_height
                                    final_width   = final_height * ratio
                                  else
                                    final_width   = min_width
                                    final_height  = final_width * ratio
                                  end
                                  "#{final_width}x#{final_height}" 
                                }
                    }, 
                    :storage => :s3,
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => ":attachment/:id/:style.:extension",
                    :bucket => 'foo_bucket' 

So I'm basically trying to do this to get a custom thumbnail width and height based on the initial image dimensions.

Any ideas?

© Stack Overflow or respective owner

Related posts about rails

Related posts about paperclip