Skip to content

Instantly share code, notes, and snippets.

@NARKOZ NARKOZ/gist:4294977
Last active Oct 26, 2015

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link

commented Apr 23, 2015

thank you, you saved my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.