Resizing Images for the web with ImageMagick

iStock_000004589853Medium-200x300.jpgOver the years I have used imagemagick and convert on Linux machines to manipulate image files and be able to resize them ready for web use in batches. Until recent times there have been few image manipulation tools that also enabled watermark embedding in batch mode. Now all the main programs, whether windows, OSX or Linux based, have batch system that enable some variety of watermarking or crediting.

However, I still find that many of these tools, including the base offering in Lightroom, Photoshop and digiKam all have their deficiencies.

I have therefore kept going with my scripts and now that I have become a semi-professional photographer (see http://kiff-backhouse.com) I need to be able to easily change the Credit Line and the Watermark for each project or Client.

The following script has evolved and is STILL very much in evloution - please feel free to use it BUT with no guarantees. I would also love to hear your feedback and even improvements...

The script

So the system is in 2 parts. A BASH script that loops through all the images of the given type within a directory and for each one:-

i) checks that it needs updating in the target directory

ii) Resizes the image to the required size. you give it a square that you want the image to fit into, for example 800x800 pixels and whether the image is portrait or landscape the long side will be 800 pixels

iii) if you have chosen to include a watermark, this is added to the center of the image

iv) if you have chosen to add a Credit, this is added to the bottom right hand corner

v) if you have chosen to have a square image, it will mount the source rectangular image on a coloured background of your choice. Why: this makes it easier to format a table/divs of images in a galllery. if you mount the image on a square card with the same colour background as the gallery, then formatting is much easier.

vi) output to target directory

How to control the script

So as normal all these options can be controlled through command line options. See below for details. However, I more often can't be bothered to type all these out (laziness is the mother of invention), so I use a control file which is stored in the current directory. The way that I organise my images, they are often held in a Project or Client top level directory and therfore I can put a control file in each directory that suits the format that that client requires.

The control file is called .resize_files and has the following parameters


iThumbSize=0 
# currently not used but will enable the creation of thumbnails as well
iPhotoSize=640 
# the square size that the photo should fit into in pixels
pFileType="jpg" 
# the file type jpg/png/gifp
Credit=/share/data/Pictures/AL/credit.png 
# the credit line. for example "Photo by Kiff Backhouse".  
# At the moment this only accepts a graphic file, which should include 
# transparency for everyhting else other than the credit.
pCopyright=/share/data/Pictures/AL/copyright.png 
# The watermark which is added to the center of the image

If any of these are not needed, just leave the right hand side blank

So here is the script source. Just cut and paste it into a file, save and then check the permissions to make it executable.

You will also need to download a script called squareup from Fred Weinhaus


#!/bin/bash
#
# resize_files by Chris Backhouse
# Published: Sept 2010
# Version: 0.2
#
# PURPOSE: to resize image files into a format
# ready for web use, apply a copyright notice and embed a watermark.
#
# CAVEAT: No guarantee that this script will work on all platforms,
# nor that trapping of inconsistent parameters is complete and
# foolproof. Use At Your Own Risk.
#
iThumbSize=0
iQuality=75
iImage=0
iThumbs=0
pFileType="jpg"
pTemp="temp"
echo `pwd`
if [ -f ".resize_files" ]; then
    echo "open Config File"
    source ./.resize_files
else
    while getopts 'r:f:t:c:' OPTION
    do
        case $OPTION in
        c)    pCopyright="$OPTARG"
            ;;
        d)    pCredit="$OPTARG"
            ;;
        r)    iPhotoSize="$OPTARG"
            ;;
        f)    pFileType="$OPTARG"
            ;;
        t)    iThumbSize="$OPTARG"
            ;;
        ?)    printf "Usage: %s: [-c Copyright-filename] [-d Credit-filename] [-r photosize] [-t thumbsize] [-f filetype jpg/png)] argsn" $(basename $0) >&2
            exit 2
            ;;
        esac
    done
    shift $(($OPTIND - 1))
fi
SRC_DIR=`pwd`
if [ "$2" != "" ]; then
    SRC_DIR=`cd $2; pwd`
    echo "Source dir="$SRC_DIR
else
    echo "Warning: No source directory specified"
    exit
fi
cd "$START_DIR"
if [ "$1" != "" ]; then
    OUT_DIR=`cd $1; pwd`
    echo "Output dir="$OUT_DIR
    if [ ! -d $OUT_DIR ]; then
        echo "WARNING:  Cannot find Destination Directory - exiting"
        exit
    fi
fi
cd "$OUT_DIR"
# Loop through files in current directory
# check if there is a smaller file in ./images
# and a thumbnail in ./thumbs
# if not then create
for filename in `find "$SRC_DIR" -iname "*.$pFileType"`; do
    #echo ">>Looping through files: $filename"
    fn=$(basename "$filename")
    fni=${fn%%.*}
    echo ">>Checking: $fn"
    if [ -f "$filename" ]; then
    #echo "images/$fni.$pFileType"
        if [ ! -f "$fni.$pFileType" ] || [ "$fni.$pFileType" -ot "$filename" ] ; then
        if [ "$fni.$pFileType" -ot "$filename" ] ; then
            echo "  updating"
        else
            echo "  converting"
        fi
        if [ "$pCopyright" = "" ] || [ "$pCredit" = "" ]; then
            pTemp="$fni.$pFileType"
        fi
        #tPS="${iPhotoSize}x${iPhotoSize}"
        #echo $tPS
        convert -resize $iPhotoSize "$filename" temp.$pFileType
        if [ "$pCopyright" != "" ]; then
            echo "  Adding copyright"
              composite -gravity center $pCopyright temp.$pFileType temp.$pFileType
        fi
        if [ "$pCredit" != "" ]; then
              echo "  Adding Credit"
              composite -gravity southeast $pCredit temp.$pFileType temp.$pFileType
        fi
        if [ "$pBorder" != "" ]; then
            convert -bordercolor #A0A0A0 -border $pBorder temp.$pFileType temp.$pFileType
        fi
        if [ "$pMontage" != "" ]; then
            echo     "  Squaring-up"
            squareup -s $pMontage -m pad -c "#A0A0A0" temp.$pFileType temp.$pFileType
        fi
        mv temp.$pFileType "$fni.$pFileType"
        echo "  output -> $iPhotoSize"
            iImage=$((iImage + 1))
        fi
    fi
done
echo "$iImage images processed"


Did you know you can hire me?

I take on projects of all sizes. From Consulting to large Development Projects.

If you're starting a new Yii project and would like some help to get setup and running or you need some help with a particular module or you just need someone to develop the whole dang thing, then just ask ...


Beginning Yii ...

I'm very excited about a new Yii Training course that has just been published.

The course is designed for anyone wanting to learn Yii, whether new to frameworks or converting from another.

Eight chapters, each having five videos of between 3 and 5 minutes, take you through the basics of the Yii Framework, building a web application step-by-step.

Save 40% - introductory offer $20.99 / £13.19 / €16.19 / A$20.99

Leave a Comment

twitterfacebookgooglelinkedin https://me.yahoo.com