Search code examples
ruby-on-railsrubyimage-processingimagemagickshrine

Blur images with Shrine and image_processing gem


I am trying to blur images in my Ruby on Rails application, using the Shrine gem. This is my uploader file:

require "image_processing/mini_magick"

class ImageUploader < Shrine
  Attacher.derivatives_processor do |original|
    magick = ImageProcessing::MiniMagick.source(original)
    {
      blurred: magick.append('-blur 0x8').resize_to_limit!(1024, 1024)
    }
  end
end

I set up my model, controller and form in the most basic way, the same as in the Shrine Getting Started tutorial - https://shrinerb.com/docs/getting-started. When I try to save an image I get following error:

*** MiniMagick::Error Exception: convert /tmp/shrine20191112-4479-1xo3vgk.jpg -auto-orient -blur 0x5 -resize 1024x1024> -sharpen 0x1 /tmp/image_processing20191112-4479-1w094sa.jpg failed with error: convert: unrecognized option `-blur 0x5' @ error/convert.c/ConvertImageCommand/893. "

Without the append('-blur 0x8') it works just fine, what am I doing wrong? My ImageMagick version is 7.0.7-11.

Btw I wouldn't mind blurring the image with libvips, I just have more experience with ImageMagick so that's what I went with.


Solution

  • You need to specify each command-line argument separately, in this case -blur and 0x8:

    magick.append('-blur', '0x8').resize_to_limit!(1024, 1024)
    

    You can also call the #blur method, which will get applied as -blur through the magic of method_missing:

    magick.blur('0x8').resize_to_limit!(1024, 1024)