Search code examples
rubyimagemagickminimagick

mini_magick thumbnail with gravity center


I want to create a padded thumbnail, like described here

This command works:

convert src.png -thumbnail '200x200>' -gravity center -extent '200x200' dst.png

But this ruby code is not working: gravity is ignored

require 'mini_magick'
image = MiniMagick::Image.open('src.png')
image.thumbnail '200x200>'
image.gravity 'center'
image.extent '200x200'
image.write 'dst.png'

What's wrong with this code?


Solution

  • You need to use combine_options with MiniMagick to roll all three of your commands together before you write it:

    require 'mini_magick'
    image = MiniMagick::Image.open('src.png')
    image.combine_options do |c|
      c.thumbnail '200x200>'
      c.gravity 'center'
      c.extent '200x200'
    end
    image.write 'dst.png'
    

    More info on the GitHub docs