Search code examples
ruby-on-railsrubyminimagick

MiniMagick Interlace isn't working


I have some custom methods to resize my images using MiniMagick

def resize_to_limit(dimensions, degrade = false)
  width, height = dimensions
  MiniMagick::Image.open(current_path).tap do |image|
    image.resize "#{width}x#{height}>"
    image.combine_options do |cmd|
      if degrade
        cmd.quality 50
      end
      cmd.alpha 'remove'
      cmd.auto_orient
      cmd.interlace 'plane'
    end
    image.format('jpg')
    image.write current_path
  end
end

When I check the resulting image using identify the Interlace is still None. Not sure if the library changed the input value, but I have tried both Plane and plane.


Solution

  • I tried to convert an image using the Metal interface:

    require 'mini_magick'
    
    MiniMagick::Tool::Convert.new do |convert|
      convert << "input.jpg"
      convert << "-interlace plane"
      convert << "output.jpg"
    end
    

    That should amount to the same thing as:

    $ convert input.jpg -interlace plane output.jpg
    

    In fact that command works as expected:

    $ magick identify -verbose input.jpg output.jpg | grep Interlace
      Interlace: None
      Interlace: JPEG
    

    You can see what commands your code is issuing by turning on debugging:

    MiniMagick.configure do |config|
      config.debug = true
    end
    

    When I run your code with debugging on, I get a bunch of commands that are working on temp files:

    MiniMagick.debug is deprecated and will be removed in MiniMagick 5. Use `MiniMagick.logger.level = Logger::DEBUG` instead.
    D, [2017-05-17T23:30:28.456080 #12301] DEBUG -- : [0.01s] identify /var/folders/dh/_zgl_f_s0t7b4wk9j27cpny80000gp/T/mini_magick20170517-12301-17vo4b8.jpg
    D, [2017-05-17T23:30:28.468335 #12301] DEBUG -- : [0.01s] mogrify -resize 500x500> /var/folders/dh/_zgl_f_s0t7b4wk9j27cpny80000gp/T/mini_magick20170517-12301-17vo4b8.jpg
    D, [2017-05-17T23:30:28.481606 #12301] DEBUG -- : [0.01s] mogrify -alpha remove -auto-orient -interlace plane /var/folders/dh/_zgl_f_s0t7b4wk9j27cpny80000gp/T/mini_magick20170517-12301-17vo4b8.jpg
    D, [2017-05-17T23:30:28.493634 #12301] DEBUG -- : [0.01s] convert /var/folders/dh/_zgl_f_s0t7b4wk9j27cpny80000gp/T/mini_magick20170517-12301-17vo4b8.jpg[0] /var/folders/dh/_zgl_f_s0t7b4wk9j27cpny80000gp/T/mini_magick20170517-12301-hks8tq.jpg
    

    One thing I can't help notice in the documentation is the line:

    On the other hand, if we want the original image to actually get modified, we can use MiniMagick::Image.new.

    So maybe try new instead of open. And then watch the debugging output to see if the commands make sense.

    It seems maybe your code is overcomplicated? Also from the documentation:

    As a handy shortcut, MiniMagick::Image.new also accepts an optional block which is used to combine_options.

    So maybe you don't need the explicit call to combine_options if using new.