Search code examples
ruby-on-railsimagemagickrails-activestorage

ImageProcessing::Error when passing crop argument to variant


I am having a model with has_one_attached :file that I want to process with a custom crop parameter. The attached file is a photo in jpg format that I use for testing.

Before, I was scaling and cropping images this way, which works as intended.

my_model.file.variant(resize_to_fill: [1440, 560, { gravity: 'Center' }])

Now I wanted to change the center / focus point for cropping the image to a custom position. The plain imagemagick command would be convert input.jpg -crop 1440x560+580+120 output.jpg, working as intended on the command line.

The docs state that you can pass almost any imagemagick commant to .variant(), which lead me to try this:

my_model.file.variant(crop: '1440x560+580+120')

This raises the following error:

ImageProcessing::Error - Source format is multi-layer, but destination format is
single-layer. If you care only about the first layer, add `.loader(page: 0)` to
your pipeline. If you want to process each layer,
see https://github.com/janko/image_processing/wiki/Splitting-a-PDF-into-multiple-images
or use `.saver(allow_splitting: true)`.

I read the manual of image_processing and understand how the pipeline works, but I am missing the part of where/how to adapt the pipeline through .variant() in order to get a result.

Am I doing something wrong here or am I just missing some simple part? Otherwise I'd go the way of writing it raw with an image_processing pipeline and work around it.

My environment consists of:

  • rails - v6.0.2.2
  • mini_magick gem - v4.10.1
  • image_processing gem - v1.10.3
  • ImageMagick 7.0.8-66

Solution

  • Try my_model.file.variant(combine_options: { crop: '1440x560+580+120')