ruby-on-railsrmagickminimagickrails-activestorage

ActiveStorage wont crop variants


I'm migrating my rails app from paperclip to ActiveStorage and it just won't accept the crop argument in a variant

this line:

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

cause this error:

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'

while eg. this works:

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

Solution

  • resize_to_fit is an ImageProcessing transformation. Rails 5.2 doesn’t use ImageProcessing; it uses MiniMagick directly instead. Rails 6 will use ImageProcessing.

    To resize to fit in Rails 5.2, append > to the resize argument:

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

    To crop, use combine_options so MiniMagick passes the gravity and crop arguments together in a single ImageMagick invocation:

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