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. |