Resizing Images with ImageMagick


I a previous article, I started a series on working with ImageMagick on the command line, but then I had to stop and deal with the massive migration project of moving my site from one server to another while simultaneously dropping it into a completely different back-end software system—madness. I'm still fixing things and cleaning up the insane sprawl of it all.

So, my last article detoured into a discussion of scripts that helped with the migration process. I'm still working on these fast, short scripts, including one I wrote this morning:

for entry in blog/*
  new=$(echo $entry | sed 's/blog\///')
  echo "Redirect 301 $entry $new"

Can you track what this loop does? The only tricky part is the new=statement that removes blog/ from the filename matched in the for statement; otherwise, it's quite straightforward.

Seriously though, let's return to ImageMagick. There are a ton of things you can do with the command-line utilities. But first, let me look at where I left off.

I'd just shown a simple example of ImageMagick command-line tools to identify the dimensions of an image and use that as the basis of coming up with a scaled HTML img tag. Here's the script:

image=$2   # needs input validation code

height=$($identify $image | cut -d\   -f3 | cut -dx -f1)
newwidth="$(echo $width \* $scale | bc | cut -d. -f1)"
newheight="$(echo $height \* $scale | bc | cut -d. -f1)"
echo "<img src=$image height=$newheight width=$newwidth>"
exit 0

(Actually, I couldn't resist tweaking it slightly if you are keeping track, but I'm still being lazy and not validating the input as of yet. You can add that code easily enough.)

In use:

$ 0.5 pvp.jpg
<img src=pvp.jpg height=152 width=485>

Okay, that's one way to make the display of the image be reduced on a Web page, but anyone who has done any work trying to speed up a Web site knows the huge problem here: reducing the container that displays an image doesn't reduce the image. The Web site visitor still has to download the original image, which is a huge waste of bandwidth and a performance hit.

So let's update the script to create a new, smaller version of the image as part of its output.

Enter the convert Command

The identify command is a great way to learn specific information about a graphical image file, but to manipulate it, you need to switch to convert.

There are a million command-line options to convert, but the one I use here is -resize, like this:

$ convert pvp-big.jpg -resize 0.5 pvp-0.5.jpg
$ identify pvp-big.jpg pvp-0.5.jpg
pvp-big.jpg JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb
pvp-0.5.jpg JPEG 1x1 1x1+0+0 8-bit DirectClass 1.1kb can see what's happened, right? The image went from 970x305 to 1x1. Yikes.

How did that happen? The problem is that I'm handing the wrong kind of parameter to the -resize option. In fact, it wants a percentage (weirdly enough), so -resize 50% or -resize 50 both work:

$ convert pvp-big.jpg -resize 50 pvp-50.jpg
$ convert pvp-big.jpg -resize 50% pvp-50%.jpg
$ identify pvp*
pvp-50.jpg[1] JPEG 50x16 50x16+0+0 8-bit DirectClass 2.01kb
pvp-50%.jpg[2] JPEG 485x153 485x153+0+0 8-bit DirectClass 44.7kb
pvp-big.jpg[3] JPEG 970x305 970x305+0+0 8-bit DirectClass 127kb

A bit of mathematics reveals that -resize 50 meant that the width was scaled to 50 pixels, with the height proportionally scaled down to a tiny 16 pixels. -resize 50%, on the other hand, accomplished the goal, scaling the image down to a reasonable 485x153.

So the script will need users to enter a proper percentage amount or otherwise compensate. To make it more interesting, let's make the output filename gain a suffix that denotes the new geometry (as ImageMagick likes to refer to the height x width values). In this instance, the goal is to have pvp-big.jpg shrink 50% and be copied as pvp-big.285x153.jpg.





Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar



Learn More

Sponsored by Storix