Home : Webmonkey : Code : Perl Thumbnail Creator


If you have chat-friend access to this site, you will have noticed that I have a large collection of pics, spanning many years. This would normally make the generation of thumbnail pictures an onerous task.

I think that laziness is half the reason that I'm a programmer. There was no way that I was going to hand create so many thumbnail images. I made the computer do it



use Image::Magick;

@inDirectories = (
$outDir = "thumbs";
$grabFrom = "web";

foreach $directory (@inDirectories){
  $directory .= "/$grabFrom";
  opendir(DIR,"$directory") || warn "Cant open directory $directory: $!";
  foreach $file (@files){
    if($file=~/jpg$/ || $file=~/gif$/ || $file=~/jpeg$/){

sub makeThumb{
  print "$path/file=$file\n";
  unless($name eq ""){

    # import, bleach and resize for shadow image
    # image proportions are kept in the resize, so
    # work out the diff in order to centre later.
    $shadow = Image::Magick->new;
    $shadow->Border(geometry=>'1x1', fill=>'#888888');

    ($w, $h) = $shadow->Get('width','height');

    # import and resize image for thumbnail
    $image = Image::Magick->new;
    $image->Border(geometry=>'1x1', fill=>'#c0c0c0');

    # stick the shadow, then the thumbnail onto large
    # canvass. Crop back to desired images, sheer it, then
    # fix the size again.
    $prebase = Image::Magick->new;

    # after sheering, we're a couple of pixels off being
    # square, so we past onto a square canvas.
    $base = Image::Magick->new;


The first thing I do is give the script a list of directories to look in - but I also tell the script that, as a standard, it should look in a particular subdirectory. In this case, that directory is called "web"

When it looks in these directories, the script has been told to process files that end in .gif, .jpg and .jpeg. When it finds such files, it passes them to the makeThumb subroutine

The thumbnails for this site are pretty fancy. They have a shadow which is actually a faded version of the original and the whole thumbnail is also skewed slightly

The makeThumb routine:

  • imports, resizes and bleaches the picture;
  • imports and resizes the original image;
  • places the bleached image on a fresh canvas;
  • places the unbleached image on top of that;
  • crops the composite image back to a decent size;
  • sheers the composite image to give it that skewed look;
  • fixes the size again;
  • pastes the whole thing onto a correct sized canvas; and
  • saves the thumbnail to the configured "thumbs" directory.

Probably more stuffing around with size than is strictly necessary, but it gets the job done.