【问题标题】:Paperclip | ImageMagick - Unable to resize image with custom height回形针 | ImageMagick - 无法使用自定义高度调整图像大小
【发布时间】:2026-01-09 02:30:01
【问题描述】:

我正在使用 Paperclip 的自定义样式来调整一些图像的大小。

自定义样式是这样定义的-

  has_attached_file :image_preview,
                                    :styles => lambda { |a|
                    { 
                      :original => "#{a.instance.model.aspect_ratio_width.to_f/2.54*300}x#{a.instance.model.aspect_ratio_height.to_f/2.54*300}"}
                  }

当看到输出时,图像被调整了宽度参数,但从来没有调整高度参数!

在日志中,我也看到在 ImageMagick 操作过程中只考虑了宽度参数 --

Command :: file -b --mime '/tmp/46711c9258f556a83aa382084f5014d120160804-14501-h9qo6j.png'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/46711c9258f556a83aa382084f5014d120160804-14501-16cbj3b.png[0]' 2>/dev/null
Command :: identify -format %m '/tmp/46711c9258f556a83aa382084f5014d120160804-14501-16cbj3b.png[0]'
Command :: convert '/tmp/46711c9258f556a83aa382084f5014d120160804-14501-16cbj3b.png[0]' -auto-orient -resize "1110" '/tmp/caba5ed9a9c1d9623acb7738fd6b8dda20160804-14501-5sjqq1'
[paperclip] saving /cases/image_previews/000/000/045/original/Goku-Blue.png

我怎样才能做到这一点?

【问题讨论】:

    标签: ruby-on-rails ruby imagemagick paperclip


    【解决方案1】:

    您可以像这样定义高度和宽度:

    has_attached_file :image_preview, :styles { original: "1920x1080>", thumb: "300x300#" }

    “#”会将中心的图像切割成给定的尺寸。

    https://github.com/thoughtbot/paperclip#quick-start

    【讨论】:

    • 宽度高度动态给定。