Skip to content

Instantly share code, notes, and snippets.

@NARKOZ
Last active October 26, 2015 06:48
Show Gist options
  • Save NARKOZ/4294977 to your computer and use it in GitHub Desktop.
Save NARKOZ/4294977 to your computer and use it in GitHub Desktop.
ImageMagick vs GraphicsMagick (Resizing to Fill a Given Space)
# gem 'mini_magick' 5217dfe
# GraphicsMagick 1.3.17
# ImageMagick 6.7.0-10
# Resize to fill (old method)
# http://www.imagemagick.org/Usage/resize/#space_fill
#
# works with ImageMagick as expected
# fails to work with GraphicsMagick as expected
def resize_to_fill(width, height)
manipulate! do |img|
img.combine_options do |cmd|
cmd.resize "#{width*2}x"
cmd.resize "x#{width*2}<"
cmd.resize '50%'
cmd.gravity 'center'
cmd.crop "#{width}x#{height}+0+0"
cmd.repage.+
end
img
end
end
# Resize to fill (new method, with '^' flag)
# http://www.imagemagick.org/Usage/resize/#fill
#
# works both with ImageMagick and GraphicsMagick as expected
def resize_to_fill(width, height)
manipulate! do |img|
img.combine_options do |cmd|
cmd.resize "#{width}x#{height}^"
cmd.gravity 'center'
cmd.extent "#{width}x#{height}"
end
img
end
end
@NARKOZ
Copy link
Author

NARKOZ commented Apr 5, 2015

-gravity option was removed from GraphicsMagick's mogrify command.
minimagick/minimagick#264 (comment)

# gem 'mini_magick' 4.2.0
# GraphicsMagick 1.3.21

def resize_to_fill_space(width, height)
  manipulate! do |image|
    MiniMagick::Tool::Convert.new do |convert|
      convert << image.path
      convert.resize "#{width}x#{height}^"
      convert.gravity 'center'
      convert.extent "#{width}x#{height}"
      convert << image.path
    end

    image
  end
end

@tonytonyjan
Copy link

thank you, you saved my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment