Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 320
Alessandro Pezzato Avatar asked Apr 30 '13 14:04

Alessandro Pezzato


1 Answers

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

like image 190
Dan Garland Avatar answered Oct 08 '22 17:10

Dan Garland