RoR - Paperclip - How to set minimal width of an attachement

血红的双手。 提交于 2019-12-24 18:33:43

问题


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#"?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/2656939/ror-paperclip-how-to-set-minimal-width-of-an-attachement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!