开发者

How to crop and then resize with Paperclip

开发者 https://www.devze.com 2023-03-01 03:20 出处:网络
I have a photo attachment which I\'m saving using Paperclip. However, I\'d like to process the photo first before saving by cropping it down and then resizing to the final size:

I have a photo attachment which I'm saving using Paperclip. However, I'd like to process the photo first before saving by cropping it down and then resizing to the final size: e.g. I have a 900x900 photo, I want to first do a central crop to 500x500, and then resize the cropped photo down to a thumbnail size of 开发者_开发技巧100x100.

The purpose of this is so that the thumbnail image wouldn't simply be a scaled down version of the 900x900 since it might be too small to even make out anything in the photo. By cropping it, we reduce a huge part of photo that can be left out and still have a meaningful scaled down thumbnail.

I know paperclip can do either crop or resize, but is there a way to combine both?

Edit: To clarify, I'm not trying to create a cropper tool where the user can interact and crop the image. For every photo that is uploaded, I want to uniformly perform a crop followed by a resize.

Thanks


There's probably a better way of doing this, but if you're using ImageMagick this way works fine.

The following will first crop in the center of the image at 500x500, then throw everything else away, then resize that new image back down to 100x100.

has_attached_file :image,  
  :styles => { :thumb => "" },
  :convert_options => { 
    :thumb => "-gravity Center -crop 500x500+0+0 +repage -resize 100x100^" },
  :default_style => :thumb


Just came across this answer, thought I'd post this as there's now an easier way to achieve the desired result simply by using the # option:

has_attached_file :image,  
  :styles => { :thumb => "100x100#" },
  :default_style => :thumb
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号