10

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')
antpaw
  • 15,444
  • 11
  • 59
  • 88

2 Answers2

25

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' })
George Claghorn
  • 26,261
  • 3
  • 48
  • 48
  • I've updated my question, I've discovered this while writing this question. I was indeed using the rails 6 API that is not available in rails 5.2. I'm sorry. I don't understand how crop works, no matter what I try it fails – antpaw May 27 '18 at 15:10
  • 1
    @antpaw, MiniMagick is a thin wrapper around ImageMagick’s command line utilities. Its `crop` transformation method doesn’t accept an array. Pass a string argument instead: `@user.image.variant(combine_options: { gravity: 'Center', crop: '180x135+0+0' })`. Read up on ImageMagick’s `-crop` option [here](https://www.imagemagick.org/script/command-line-options.php#crop). – George Claghorn May 27 '18 at 15:55
  • 3
    thank you so much for telling me about `combine_options` this is what I ended up using `combine_options: {resize: '180>', gravity: 'Center', crop: '180x135+0+0'}`. Please submit this as an answer so I can accept it – antpaw May 28 '18 at 10:19
  • When upgrading to Rails 6 and removing the combine_options, doesn't that then change the hash key for the variant? So if you upgrade from Rails 5 to Rails 6, all your images get recreated? – John Pollard Mar 30 '21 at 18:43
17

For Rails 6.0 users:

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

I'm writing this for those who like me didn't know how to use options in variants.

Pascal
  • 1,158
  • 17
  • 20
  • 2
    This helped me today. Thank you so much! I was writing `object.image.variant(resize_to_fill: [180, 135], gravity: 'North')` for the longest time but just realized it wasn't working now. – Alexander May 14 '21 at 21:55