简体   繁体   English

如何使用回形针裁剪然后调整大小

[英]How to crop and then resize with Paperclip

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: eg 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.但是,我想先处理照片,然后通过裁剪然后调整到最终大小来保存:例如,我有一张 900x900 的照片,我想先将中央裁剪为 500x500,然后将裁剪后的照片缩小到 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.这样做的目的是使缩略图图像不会只是 900x900 的缩小版本,因为它可能太小而无法辨认出照片中的任何内容。 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.这样做可能有更好的方法,但如果您使用的是 ImageMagick,这种方法可以正常工作。

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.以下将首先在图像中心以 500x500 进行裁剪,然后丢弃所有其他内容,然后将新图像的大小调整回 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM