Attachment_Fu thumbnail width
-
21-08-2019 - |
Question
Any ideas on how to get the width and height of the thumbnail image? photo.width returns the original photos width. I am storing the width of the thumbnail in the database, I'm just not sure how to access that object.
Doesn't work:
<%= image_tag photo.authenticated_s3_url(:medium),
:width => photo.width,
:height => photo.height %>
Neither does this:
<%= image_tag photo.authenticated_s3_url(:medium),
:width => photo.authenticated_s3_url(:medium).width,
:height => photo.authenticated_s3_url(:medium).height %>
Solution
By default, attachment_fu only loads the parent photo from a table to get info like the url. When you pass it a thumbnail option it simply appends the appropriate size to the end of the filename before the extension.
If you want to get the size, you're going to need to look it up in the table with something like, Photo.find_by_parent_id(photo.id, :conditions => ['thumbnail = ?', 'thumbnail_class_name']).width
You're better off knowing the width of a photo beforehand, at least if you're going to be using this for anything that would be run often.
OTHER TIPS
:thumbnails => { :crop_200x200 => 'c200x200',
:default_200x200 => '200x200',
:ex_200x200 => '200x200!',
:gt_200x200 => '200x200>',
:lt_200x200 => '200x200<'}
To get the height and width this is what I would do
tname = 'crop_200x200'
height= @picture.thumbnails.select{ |r| r.thumbnail == tname }.first.height
width= @picture.thumbnails.select{ |r| r.thumbnail == tname }.first.width