arrow-left arrow-right brightness-2 chevron-left chevron-right facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Teaching Image Magick who's boss
1 min read

Teaching Image Magick who's boss

A few days ago I asked if anyone knew how to get either Image Magick or NetPBM to create square thumbnails built from the largest square that can fit inside the center of the original image?

While I received a few e-mails from others wanting the same, no one had a solution for me and so I had to fight off empathy and figure it out on my own (even though I was told it couldn’t be done). I would have typed this up sooner, but a good friend of mine is currently working with a new social-networking startup and thought that I might be able to use this (+ some other Image Magick/PHP voodoo) to make a quick buck, and so I had to wait for that situation to resolve itself.

Without getting into the details and at the same time gracefully skirting around how much time I spent on this, I’ll just show you the command and tell you what you need to change if you want to do it for a larger/smaller thumbnail.

convert filename.jpg -thumbnail x170 -resize '170x<' -resize 50% -gravity 
center -crop 85x85+0+0 +repage newfile.jpg

The above is for an 85×85 pixel thumbnail. Notice that the first two numbers (the 170s) are twice the width/height of the final thumbnail I wanted to create; given those relations you should be able to deduce what you’d need for a different sized thumbnail. Note that this command sequence also seems to work quite well for portrait images (i.e., where the height is greater than the width).

You've successfully subscribed to Justin Blanton.
Success! Your account is fully activated, you now have access to all content.