简体   繁体   English

Paperclip:如何仅在图像足够大的情况下调整图像大小?

[英]Paperclip: How to resize an image only if it is big enough?

I use Paperclip to resize pictures like this:我使用回形针来调整图片的大小,如下所示:

class Asset < ActiveRecord::Base
  has_attached_file :asset, :styles => { :thumb => "80x80#", 
                                         :medium => "1280x800>" }, ...

When the size of the original picture is 32x32 :当原始图片大小为32x32时:

  1. The resulting medium picture is of the same size (ie 32x32 ), but the file is a bit different and the picture looks a bit changed.生成的medium图片大小相同(即32x32 ),但文件有点不同,图片看起来有点变化。 Why is that?这是为什么?

  2. The resulting thumb size is 80x80 (it looks stretched to fit this size).生成的thumb大小为80x80 (它看起来被拉伸以适合这个大小)。 How could I avoid resizing the picture when it's too small.图片太小时如何避免调整大小。 Assume that original picture's dimensions are in the width and height variables.假设原始图片的尺寸在widthheight变量中。

(1) Is probably because Paperclip is decoding a JPEG and then writing/encoding a new JPEG. (1) 可能是因为 Paperclip 正在解码 JPEG,然后写入/编码新的 JPEG。 JPEG is a lossy format so each encoding degrades the image. JPEG 是一种有损格式,因此每次编码都会降低图像质量。 You could use the convert_options to adjust the JPEG quality but you'll probably have to accept some degradation in your JPEGs.您可以使用convert_options来调整JPEG 质量,但您可能不得不接受 JPEG 的质量下降。

(2) is because Paperclip is doing as it's told. (2) 是因为 Paperclip 正在按照它的说法行事。 Paperclip uses ImageMagick to do the heavy lifting and the style dimensions are just ImageMagick geometry strings with one modification : Paperclip 使用 ImageMagick 来完成繁重的工作,样式尺寸只是ImageMagick 几何字符串,经过一次修改

Paperclip also adds the “#” option (eg “50x50#”), which will resize the image to fit maximally inside the dimensions and then crop the rest off (weighted at the center). Paperclip 还添加了“#”选项(例如“50x50#”),它将调整图像大小以最大程度地适应尺寸,然后裁剪 rest(在中心加权)。

Your :thumb style uses "#" so you're telling Paperclip that you want a 80x80 image even if the image has to be scaled and cropped to get there.你的:thumb样式使用“#”,所以你告诉 Paperclip 你想要一个 80x80 的图像,即使图像必须被缩放和裁剪才能到达那里。 You can drop the "#" from the dimensions string and, if desired or appropriate, add one of the the other modifiers .您可以从维度字符串中删除“#”,如果需要或适当,可以添加其他修饰符之一。

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

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