in reply to GD Module

This is a direct code fragment from some code I use to generate thumbnails (no doubt merlyn will have something to say about it...)
my $globalPreviewMaxDimension = 100; sub util_photo_GenerateThumbnail { my ($source, $destination) = @_; my $preview = Image::Magick->new; my $x = $preview->Read ($source); confess "$x" if "$x"; my ($xSize, $ySize) = $preview->Get ('width', 'height'); # Get the p +ictures dimensions # # Calculate dimensions of thumbnail # my $scaleFactor; my $previewX; my $previewY; # # Only scale if either axis is larger than the preview size # if ($xSize > $globalPreviewMaxDimension || $ySize > $globalPreviewMa +xDimension) { if ($xSize > $ySize) { $scaleFactor = $globalPreviewMaxDimension / $xSize; $previewX = $globalPreviewMaxDimension; $previewY = int ($ySize * $scaleFactor); } else { $scaleFactor = $globalPreviewMaxDimension / $ySize; $previewY = $globalPreviewMaxDimension; $previewX = int ($xSize * $scaleFactor); } $x = $preview->Scale (width=>$previewX, height=>$previewY); confe +ss "$x" if "$x"; $xSize = $previewX; $ySize = $previewY; } $x = $preview->Border (color=>'black', width=>1, height=>1); confess + "$x" if "$x"; $x = $preview->Set (quality=>90); confess + "$x" if "$x"; $x = $preview->Write ($destination); confess + "$x" if "$x"; return ($xSize, $ySize); }
It puts a 1 pixel black border around the edge. Read doesn't care what the source is, and will write the thumbnail back out in the format that it was read as. The inputs are the name of the source file to be thumbnailed, and the output thumbnail name. Returns the dimensions of the thumbnails.

--Chris

Updated to fix the horrendous tabbing.

Replies are listed 'Best First'.
RE: Re: GD Module
by dvst8 (Initiate) on Jun 20, 2000 at 19:18 UTC
    Thanks Chris. Like I said, I'm new to Perl, so I'm trying to decipher your code to figure out what it's doin... I take it Image:Magik is a Module that I have to d-load..? Or do you think it comes with ActiveState? Will this work with any image format? Will it work in PerlScript? Is the following usage correct?
    $fromPath = "c:\temp\pic1.jpg"; $toPath = "c:\images\thumb1.jpg"; @newDims = util_photo_GenerateThumbnail($fromPath, $toPath)
    Thanks again
      Image::Magick is a Perl interface into libmagick. You can get it from CPAN (or click the link to the left). It will work with any image format that libmagick supports (GIFs are a little funny these days. I don't remember if they'll work or not).

      The code example will (should) work if you change the '\' to '\\', since you need to escape slashes. Or, you could use single quotes instead. Doing something with the returned values is up to you. If you're going to discard them, you could just leave the variable off.

      --Chris

      Updated: I was thinking that ImageMagick used GD, but it doesn't. It's a standalone library for image manipulation. GD is for doing plots and graphs. Sorry about that.
        thank you much appreciated! /d8
        hmmmm
        by dvst8 (Initiate) on Jun 20, 2000 at 23:03 UTC
        hmmmm
        by dvst8 (Initiate) on Jun 20, 2000 at 23:04 UTC
        hmmmm
        by dvst8 (Initiate) on Jun 20, 2000 at 23:08 UTC
(jcwren) RE: Re: GD Module
by jcwren (Prior) on Jun 21, 2000 at 02:25 UTC
    Sorry. The confess is from the Carp module. Just change those to 'die' instead, and you'll be fine.

    --Chris