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

标签 ruby-on-rails ruby-on-rails-3 imagemagick paperclip

我使用回形针来调整图片的大小,如下所示:

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

当原图大小为32x32时:
  • 结果 medium图片大小相同(即 32x32 ),但文件有点不同,图片看起来有点变化。这是为什么 ?
  • 结果 thumb大小为 80x80 (它看起来被拉伸(stretch)以适合这个尺寸)。
    图片太小时如何避免调整大小。假设原始图片的尺寸在widthheight变量。
  • 最佳答案

    (1) 可能是因为 Paperclip 正在解码 JPEG,然后写入/编码新的 JPEG。 JPEG 是一种有损格式,因此每次编码都会降低图像质量。您可以使用 convert_options 调整JPEG quality但您可能不得不接受 JPEG 格式的一些劣化。

    (2) 是因为 Paperclip 正在按照它的说法行事。 Paperclip 使用 ImageMagick 完成繁重的工作,样式尺寸仅为 ImageMagick geometry stringsone modification :

    Paperclip also adds the “#” option (e.g. “50x50#”), which will resize the image to fit maximally inside the dimensions and then crop the rest off (weighted at the center).



    您的 :thumb style 使用“#”,所以你告诉 Paperclip 你想要一个 80x80 的图像,即使图像必须被缩放和裁剪才能到达那里。您可以从维度字符串中删除“#”,如果需要或适当,添加 the other modifiers 之一.

    关于ruby-on-rails - Paperclip:如何仅在图像足够大的情况下调整图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6729643/

    相关文章:

    ruby-on-rails - 如何通过 Deliver_now 以 HTML 形式发送电子邮件而不需要单独的类

    ruby-on-rails - Rails/Bootstrap/HAML - 如何将此代码转换为 HAML 显示 Flash 消息?

    ruby-on-rails - Ruby on Rails : How do I change the behavior of RecordNotFound?

    ruby-on-rails - 用于 RDF 存储的 Rails 应用程序架构

    ruby-on-rails-3 - 使用 Jasmine 和 Rails 3.1 测试 Coffeescript

    imagemagick - C++ TIFF(原始)到 JPEG : Faster than ImageMagick?

    ruby-on-rails - 无效请求 : Invalid HTTP format, 在 Rails 中解析失败

    ruby-on-rails - 将变量从 .txt 文件插入 ERB

    php - Imagemagick 裁剪不工作

    php - Imagemagick convert -fill 不会改变特定调色板区域的边缘