ActiveStorage不会裁剪变种

时间:2018-05-26 21:47:03

标签: ruby-on-rails rmagick minimagick rails-activestorage

我正在将我的rails应用程序从paperclip迁移到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')

2 个答案:

答案 0 :(得分:12)

resize_to_fitImageProcessing转化。 Rails 5.2不使用ImageProcessing;它直接使用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' })

答案 1 :(得分:4)

对于Rails 6.0用户:

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

我正在为那些像我一样不知道如何在变体中使用选项的人写这本书。