ActiveStorage 不会裁剪变体

ActiveStorage wont crop variants

我正在将我的 rails 应用程序从回形针迁移到 ActiveStorage,它只是不接受变体中的 crop 参数

这一行:

@user.image.variant(crop: [180,135])

导致此错误:

Errno::ENOENT (No such file or directory @ rb_sysopen - /var/folders/dd/dy3xgqrs2vv6h97ckrtmrb4m0000gn/T/mini_magick20180526-14598-njz21n.jpg):

activestorage (5.2.0) app/models/active_storage/variant.rb:130:in `initialize'
activestorage (5.2.0) app/models/active_storage/variant.rb:130:in `open'
activestorage (5.2.0) app/models/active_storage/variant.rb:130:in `upload'
activestorage (5.2.0) app/models/active_storage/variant.rb:88:in `block in process'
activestorage (5.2.0) app/models/active_storage/variant.rb:110:in `open_image'
activestorage (5.2.0) app/models/active_storage/variant.rb:85:in `process'
activestorage (5.2.0) app/models/active_storage/variant.rb:53:in `processed'
activestorage (5.2.0) app/controllers/active_storage/representations_controller.rb:12:in `show'

而例如。这有效:

@user.image.variant(resize: '180x135')

resize_to_fit 直接是 ImageProcessing transformation. Rails 5.2 doesn’t use ImageProcessing; it uses MiniMagick。 Rails 6 将使用 ImageProcessing。

要调整大小以适应 Rails 5.2,请将 > 附加到 resize 参数:

@user.image.variant(resize: '180x135>')

要裁剪,请使用 combine_options 以便 MiniMagick 在单个 ImageMagick 调用中一起传递 gravitycrop 参数:

@user.image.variant(combine_options: { gravity: 'Center', crop: '180x135+0+0' })

对于 Rails 6.0 用户:

object.image.variant(resize_to_fill: [180, 135, { gravity: 'North' }])

我写这篇文章是为了那些像我一样不知道如何在变体中使用选项的人。