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

发布于 2024-11-04 04:38:51 字数 357 浏览 9 评论 0原文

我有一个照片附件,正在使用回形针保存。但是,我想在保存之前先处理照片,方法是裁剪照片,然后调整大小到最终尺寸: 例如,我有一张 900x900 的照片,我想首先将中央裁剪为 500x500,然后将裁剪后的照片大小调整为 100x100 的缩略图大小。

这样做的目的是使缩略图图像不仅仅是 900x900 的缩小版本,因为它可能太小,甚至无法辨认照片中的任何内容。通过裁剪,我们减少了照片中可以省略的很大一部分,但仍然具有有意义的缩小缩略图。

我知道回形针可以裁剪或调整大小,但有没有办法将两者结合起来?

编辑:为了澄清,我并不是想创建一个裁剪工具,用户可以在其中交互和裁剪图像。对于上传的每张照片,我想统一执行裁剪,然后调整大小。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-11-11 04:38:51

可能有更好的方法来做到这一点,但如果您使用 ImageMagick,这种方法效果很好。

下面将首先以 500x500 的尺寸在图像的中心进行裁剪,然后丢弃其他所有内容,然后将新图像的大小调整回 100x100。

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

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
吃→可爱长大的 2024-11-11 04:38:51

刚刚遇到这个答案,我想我应该发布这个答案,因为现在有一种更简单的方法可以通过使用 # 选项来实现所需的结果:

has_attached_file :image,  
  :styles => { :thumb => "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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文