Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Imagemagick how to apply gravity and extent to only short images

I have a web page where I list images that should be 600px by 600px (width & height). So I resize all big images to 600x600 using imagemagick, which works fine.

However, some images, when I resize them (in order to keep their aspect ratio), get resized to a size smaller than 600px in height (for example 600x450). So I tried to fill the needed height by using imagemagick options (gravity, background, and extent) like so :

image.combine_options do |img|
    img.crop('1200x1200+0+0')
    img.resize '600x600'
    img.background 'white'
    img.extent '600x600'
    img.gravity 'center'
end

That gave me what I want for these kind of images (short images). However, this effect of centering is applied to the other images (bigger ones) as well ! Is there any way to prevent that ?

Update:

Just to show you what I mean, I made up this example... if I don't add extent and gravity the image will start from the top left corner

enter image description here

but if I add extent and gravity (center) the image will start from the center, something like this :

enter image description here

I want only images that result on resized images smaller than 600x600 to be centered, but in the example as you see even an image that get resized to exact 600x600 get centered !! that's not what I want

the solution :

I end up using shell command using system function in ruby

system "convert #{image.path} -crop 1024x1024+0+0 -resize 600x600 -background white -gravity center -extent 600x600 output.png"

and that worked fine! I don't know why minimagick wasn't working correctly, but I just get rid of that gem from my gemfile which is fine too.

like image 899
medBouzid Avatar asked Dec 14 '25 14:12

medBouzid


1 Answers

Here is your command in command line Imagemagick with proper resetting of the virtual canvas after the crop.

convert image -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result

Here are two results with slightly different resize arguments. It looks like your commands are using the equivalent of the ^ flag set on the resize argument.

Input:

enter image description here

Proper result without the ^ flag: (Note padded with white)

convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600 -background white -gravity center -extent 600x600 result1.png


enter image description here

Result with the ^ flag: (Note cropped)

convert wiki.png -crop 1200x1200+0+0 +repage -resize 600x600^ -background white -gravity center -extent 600x600 result2.png


enter image description here

The above is Unix syntax. For Windows double the ^ to ^^, since ^ is an escape character in Windows.

Perhaps your issue is with minimagick and not Imagemagick. You can check by testing my command line.

like image 60
fmw42 Avatar answered Dec 18 '25 08:12

fmw42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!