views:

163

answers:

2

Hi, my layout's requirement is to keep all thumbnails at 80px height, not higher, not smaller. In my model I set the style to :thumb=> "500x80>", so basically almost every picture which is not too wide gets its perfect miniature with 80px height. Sometimes, however, my pictures are narrow and high, so the thumb can have unclickable dimensions of like 5x80. So I dont want to crop pictures as long as thumbnails are not getting crazy narrow, but I think I can make a little sacrifice and crop them if thumb's width is getting smaller than 25px.

So my questions is - is it possible in paperclip to set minimal proportions of a picture by which the style will be "500x80>" and beyond that it will turn to sth like "25x80#"?

A: 

I'm not sure how you could accomplish this using just paperclip - feels like there should be someway to do it doesn't it?

Paperclip is just using imagemagick in the background (http://www.imagemagick.org/Usage/resize/#shrink) you could cron a job that uses image magic to grow those pesky narrow images on a nightly basis.

It's a hack, but best idea I can offer.

Good luck.

Bobby B
A: 

I found a nice solution somewhere in the internet a couple of weeks ago. I forgot where, sorry. But it looks like this:

has_attached_file :img, :styles => {:thumb => [Proc.new { |instance| instance.resize }, :jpg]}


def resize     
@geo_original = Paperclip::Geometry.from_file(img.to_file(:original))

ratio = @geo_original.width/@geo_original.height  

if ratio < 0.4 or ratio > 1.375
    # Image very high or very wide
    "110x80#"   
else
    # Average dimensions
    "110x80>"
end
end
sNiCKY