2
votes

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.

1
It would be interesting to know the actual imagemagick shell command, that's generated by this dsl construct.FaZe Unempl0yedd
@FaZeUnempl0yedd it should soemthing like this : convert image -crop 1200x1200+0+0 -resize 600x600 -background white -gravity center -extent 600x600 result also I updated my question to show and example-like of what I getmedBouzid
I would suggest you resize your input images to 1024x1024 first, then crop and add +repage. The repage is need for PNG image, but not for JPG to remove the virtual canvas. The resize will optimize the space your image occupies. For smaller images, this will prevent too much padding. For larger images, it will avoid center cropping.fmw42
@fmw42 your suggestion of resizing to 1024x1024 didn't work for me, it makes the image skinny and centered with a white padding ( right and left sides) .... but the essential is that the simple command has worked so I prefer to use the shell command :) I accepted your question below, thank you again! much appreciated your helpmedBouzid

1 Answers

1
votes

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.