Creating Photo Galleries With ImageMagick

Part of creating a web site often involves creating photo galleries of the clients products. Usually this means the designer recieves a number of photos and must resize them into thumbnails, then link them to larger pictures.

This is a boring and laborious process; photo gallery software is available (e.g. Gallery etc.), but is excessive for simple projects that are not updated often.

The Solution

This web designer spent an evening (and early morning) hacking and came up with: ‘Thumbnailor’ (spelling intentionally incorrect). Thumbnailor is a small script for resizing lots of images, it was written as a Linux script that requires ImageMagick and is—of course—distributed under the GNU GPL.

You don't need to know what all that means. Although a working knowledge of Linux (or similar OS) helps.

Usage

To use Thumbnailor, complete the following steps: —

  1. Get a copy of ImageMagick. If you’re unsure whether you have it already, try typing: convert at a command line. If you get information on how to use convert, and details of ImageMagick’s web site, then it is installed.
  2. Download the file attached to the article, save it as ‘thumbnailor’, somewhere your Linux system will be able to execute it (e.g. ~/bin/thumbnailor or /bin/thumbnailor, recommended is the bin directory in your home directory, create this if it does not exist).
  3. Open up a terminal and navigate to the directory where Thumbnailor is stored, e.g. cd ~/bin/. Then give permission for the script to execute by typing: chmod +x thumbnailor. You may need to be the root user to do this, depending on where you saved the file.
  4. Open a terminal and type: ‘thumbnailor’. The program should print an error message and usage statement. This means we have successfully installed the script. Possible problems at this stage are: —
    • A ‘Permission Denied’ error is printed to the screen. This is caused by incorrect permissions, has step two been completed successfully?
    • Command not found’ is printed to the screen. Your Linux system doesn't look for programs where you put the script. Not an issue; type the entire path to the script, not just the name, e.g. ~/downloads/thumbnailor. Do this every time the script needs to be run.
  5. Make a copy of some images you’d like to make a photo gallery from (it's not wise to use originals, and remember there's no warranty with this script!).
  6. Using the command-line: navigate to where the images are stored. For example, if they are stored under ~/images/thumbtest/ type: cd ~/images/thumbtest
  7. Type: thumbnailor "*.jpg" 150 70 . Assuming the images you wished to convert were JPEG’s, you should see a new directory named ‘thumbs’ appear. The thumbnails are in there, open the directory and view some of the files using your favourite image viewer. If you recieve a: ‘command not found’ error, the full path to thumbnailor needs to be entered when running it, e.g. ~/downloads/thumbnailor "*.jpg" 150 70.
  8. As shown, Thumbnailor needs three arguments to work. These are: —
    1. A list of files, e.g. "*.jpg". The double-quotes must be included or strange things happen. The script has only been tested with JPEG files, but may work with other types too.
    2. The size of the thumbnail, e.g. 150. Thumbnailor currently generates square thumbnails only. Resizing a rectangular image using ImageMagick is easy, the need for Thumbnailor is in the cropping.
    3. Quality of the output files, e.g. 70. A value between 1 and 100 is required.
  9. Try Thumbnailor for yourself, test it with different settings.

Conclusion

This should be enough information to start using Thumbnailor. This is the first time the script has been published so probably has a number of bugs, but if used responsibly could be very useful.

It was created for a production project, where it was used to good effect, hopefully others can also put it to good use too, and if you find a problem: let me know!

During that production project I also used ImageMagick from the Linux command-line to resize the larger images in the gallery. This will be explored in another article, later in this series.

AttachmentSize
thumbnailor1.88 KB

About the Author

Liam McDermott is the technical bod at The Webmaster Forums. He also writes articles and loves dallying with Drupal. His business site is InterMedia.