Hagbone has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise ones ....

I am an Image::Magick newbie, and have a question for those who may be familiar with the program/module.

I've searched the monastery to no avail, but did notice that some of the results/comments indicated that the ImageMagick "proper" documentation is, well, lame (and I think I'd have to agree).

I'm trying to produce thumbnails, and the code that follows is working .... my goal is to produce a page of *light weight* on-the-fly thumbnails (that link to the full size image). The full size images are pretty light to start with (800 X 600 pixel gif's that average 25K to 50K). What I've found is (using the code below), if the thumbnails are resized to 1/4 (or less) of the original size, the image weight is always less then the original. If, however, I resize the image to half the original size, the thumbnail weight is often *double* the original.

This makes me think that maybe I'm missing some sort of compression issue in my code, 'cause it sure seems to me that a smaller version of the original should be lighter-weight then the original. Am I missing something, or is the thumbnail idea only appropriate when the reduction is greater then 1/4 the original? (BTW: linux/apache setup)

sub MakeThumb { my $src = Image::Magick->new; $src->Read("$impath/$file"); #grab size my ($width, $height) = $src->Get('width', 'height'); #reduce size $width = $width/$div; $height = $height/$div; # Create the thumbnail based on reduced size my ($thumb,$x,$y) = create($src,$width,$height); # Save thumbnail to file(CH this works) $thumb->Write("$file"); print qq~<img src="$imurl/$file" width="$width" height="$height" b +order="0"><br>~; #get rid of temp file unlink ("$file") or die "can't unlink $file!\n"; }

Replies are listed 'Best First'.
•Re: Image::Magick - thumbnail compression question
by merlyn (Sage) on Apr 21, 2003 at 23:19 UTC
    Reducing a size of an image isn't always a guarantee that it'll take fewer bytes to store in a given image format.

    You might consider using JPGs and reducing the quality until the size is sufficiently reduced.

    By the way, thumbnails are easier than you show. To make a thumbnail that is 50%:

    my $thumb = $src->Clone; $thumb->scale(Geometry => '50%');
    or to make a thumbnail that is less than 100x100 (in both directions), proportionally reduced (that is, if it's 400x200 originally, it'll be 100x50 when you're done):
    my $thumb = $src->Clone; $thumb->scale(Geometry => '100x100');
    It just Does The Right Thing.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Randal has already given you excellent advice. One more point: If JPG isn't suitable for your thumbnails, you might want to try out PNG. With a good PNG encoder, I manage to beat GIF file sizes most of the time, often substantially. Look for pngcrush to get even smaller files.

Re: Image::Magick - thumbnail compression question
by swngnmonk (Pilgrim) on Apr 23, 2003 at 02:56 UTC
    As a side note - You might want to take a look at Imager. It doesn't get as much attention as Image::Magick, but it does the job well, and it has a much smaller footprint - a useful trait for any library used in a CGI environment.