Thumbnailing Images (Perl Cookbook, 2nd Edition)

## 15.19. Thumbnailing Images

### 15.19.1. Problem

You have a large image and you want to create a smaller version of that image, the thumbnail. For example, on a web site you might use a thumbnail as a preview to let readers see the basic image before they decide whether to download the larger original.

### 15.19.2. Solution

Use the Image::Magick module from CPAN:

>     use Image::Magick;

>     $image = Image::Magick->new( );
>     $image->Read($ORIGINAL_FILENAME);
>     $image->Resize(geometry => '120x90');
>     $image->Write($THUMBNAIL_FILENAME);

### 15.19.3. Discussion

The Image::Magick module is a frontend to the ImageMagick suite, available from http://imagemagick.sourceforge.net. It handles many complex and powerful image manipulations, but here we're only concerned with the very simple resizing.

The `Resize` method's `geometry` parameter indicates the new geometry (`width` `x`height). You can also specify percentages: '`75%`' to resize each axis proportionally to 3/4 of its original size, or '`10%x30%`' to resize the X axis to 10% of its original value and the Y axis to 30%.

You can also specify a filter to use and how much to blur or sharpen the image with that filter:

>     $image->Resize(geometry => '120x90',
>                    filter   => 'Gaussian',
>                    blur     => 2);

A `blur` value greater than 1 indicates blurring; a value less than 1 indicates sharpening. The valid filters are `Point`, `Box`, `Triangle`, `Hermite`, `Hanning`, `Hamming`, `Blackman`, `Gaussian`, `Quadratic`, `Cubic`, `Catrom`, `Mitchell`, `Lanczos`, `Bessel`, and `Sinc`.

### 15.19.4. See Also

The documentation for the Image::Magick modules; Perl Graphics Programming

Copyright © 2003 O'Reilly & Associates. All rights reserved.