NAME

Image::Resize - Simple image resizer using GD

SYNOPSIS

use Image::Resize;
$image = Image::Resize->new('large.jpg');
$gd = $image->resize(250, 250);

ABSTRACT

Resizes images using GD graphics library

DESCRIPTION

Despite its heavy weight, I've always used Image::Magick for creating image thumbnails. I know it can be done using lighter-weight GD, I just never liked its syntax. Really, who wants to remember the lengthy arguments list of copyResized() or copyResampled() functions:

$image->copyResampled($sourceImage,$dstX,$dstY,
                    $srcX,$srcY,$destW,$destH,$srcW,$srcH);

when Image::Magick lets me say:

$image->Scale(-geometry=>'250x250');

Image::Resize is one of my attempts to make image resizing easier, more intuitive using GD.

METHODS

new('path/to/image.jpeg')'

Constructor method. Creates and returns Image::Resize object. Expects path to the image as its first and only argument. Supported image formats are jpeg, png and gif. Starting 0.3 extension is no longer required. If it's missing, it will attempt to retrieve the file type using Image::Info, so Image::Info has to be installed to be able to process images without any extension.

resize($width, $height);
resize($width, $height, $constraint);

Returns a GD::Image object for the new, resized image. Original image is not modified. This lets you create multiple thumbnails of an image using the same Image::Resize object.

First two arguments are required, which define new image dimensions. By default resize() retains image proportions while resizing. This is always what you expect to happen. In case you don't care about retaining image proportions, pass 0 as the third argument to resize().

Following example creates a 120x120 thumbnail of a "large" image, and stores it in disk:

$image = Image::Resize->new("large.jpg");
$gd = $image->resize(120, 120);

open(FH, '>thumbnail.jpg');
print FH $gd->jpeg();
close(FH);
gd()

Returns internal GD::Image object for the original image (the one passed to Image::Resize->new).

width()
height()

Returns original image's width and height respectively. If you want to get resized image's dimensions, call width() and height() methods on the returned GD::Image object, like so:

$gd = $image->resize(120, 120);
printf("Width: %s, Height: %s\n", $gd->width, $height);
type()

Returns suggested file-extension for the image. It's always the same as the extension of the original image. type() is particularly useful if original image didn't have an extension.

BUGS AND LIMITATIONS

  • To fool Image::Resize all you need is a false file extension. This is because it tries not to rely on Image::Info, but tries to guess the type by looking at extension alone. It will invoke Image::Info only if an extension is missing. I realize it may be better if it ignores file extensions altogether, and relies solely on Image::Info. I just didn't want to add a prerequisite to fix a rarely encountered problem.

  • Someone needs to have a look at my scaling routine. I'm not sure if it's compatible with Image::Magick's Scale(). Needs testing.

CREDITS

Thanks to Paul Allen <paul.l.allen AT comcast.net> for trueColor(1) tip. Now GD::Resize should work fine for photographs too.

SEE ALSO

Image::Info, GD, Image::Magick

AUTHOR

Sherzod B. Ruzmetov, <sherzodr@cpan.org> http://author.handalak.com/

COPYRIGHT AND LICENSE

Copyright 2005 by Sherzod B. Ruzmetov

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.