Recipe 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 (widthxheight). 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



Perl Cookbook
Perl Cookbook, Second Edition
ISBN: 0596003137
EAN: 2147483647
Year: 2003
Pages: 501

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net