Add a Ruler to Catalog Images

An e-commerce site with thousands of product images needs to provide a visual cue for the size of each product. The solution: a shell scripts that uses Imagemagick utilities to add a ruler.

Imagemagick is a free software suite to create, edit, and compose bitmap images.
The script (shown below) uses three Imagemagick utilities: identify, convert and composite.

First you need an image of a ruler that is at least as long as the longest product. It's a good idea to use colors in the ruler which blend properly with the page theme. For a site using Acquia Slate theme, we used this ruler:
.
You will have to scale the ruler so it corresponds with the actual size of the catalog images (using, for example, Gimp).


Image with ruler added


When the image is shown at a smaller scale on a catalog page, the ruler blends in with the background.

Product Image on Catalog Page



The first section of the script sets some constants, including the dimensions of the product image and of the ruler.

Next we cut the ruler to match the width of the product image. A blank canvas is created with dimensions equal to the product image plus the ruler.
Then the product image and ruler are pasted on to the canvas.

PRODUCT=$1

RULER=/tmp/ruler.jpg
CANVAS=/tmp/canvas.jpg
RULER_TMP=/tmp/ruler.$$
trap "rm -f $RULER_TMP $CANVAS" 0 1 2 3 15
RULER_HEIGHT=`identify -format %h $RULER`
WIDTH=`identify -format %w $PRODUCT`
PRODUCT_HEIGHT=`identify -format %h $PRODUCT`
PRODUCT_HEIGHT=$[$PRODUCT_HEIGHT+$RULER_HEIGHT]

# Cut ruler to width of product image
convert -crop "$WIDTH"x"$RULER_HEIGHT" $RULER $RULER_TMP

# Create canvas
convert -size "$WIDTH"x"$PRODUCT_HEIGHT" xc:white $CANVAS

# Add product image to canvas
composite $PRODUCT $CANVAS -gravity north $CANVAS

# Add ruler to canvas
composite $RULER_TMP $CANVAS -gravity south $PRODUCT

Script to add ruler

Summary: 
An e-commerce site with thousands of product images needs to provide a visual cue for the size of the product. The solution: a shell scripts that uses Imagemagick utilities to add a ruler.