Saving The New Index Image - Page 24
December 18, 2001
We've drawn all of the thumbnails in the index image, so now it's
time to save it to a file. We do this by creating a new file
handle and printing the results of the jpeg method
to the file. And we're done.
open(IMAGE,">index.jpg")
|| die "Cannot open index.jpg for write: $!\n";
print IMAGE $indexImage->jpeg(100);
Example 1
Now that we've worked through building the application in a
piecemeal form, let's take a look at the whole script. Cut and
paste the source code below into an editor. Make sure you have
the GD Perl module installed correctly. The script must be run
from the command line. It requires two parameters. The first is
the path to the directory where the JPEG images exist that you
want to place in an index image. The second parameter is the
title that will be placed at the top of the index image.
ex. index.pl images/ 'Seaworld 2000 - Miami, Florida'
use GD;
use strict;
use POSIX;
die "Syntax: index.pl <directory> <title>\n\n"
unless @ARGV == 2;
chomp(my $dir = $ARGV[0]);
chomp(my $title = $ARGV[1]);
opendir(DIR,$dir) || die "Cannot open directory $dir: $!\n";
my @images = grep -f, grep /\.jpg$/i, map "$dir/$_", readdir DIR;
closedir(DIR);
my $imagesPerLine = 5;
my $rows = ceil(scalar(@images) / $imagesPerLine);
my $titleBorder = 50;
my $vBorder = 40;
my $hBorder = 20;
my $maxThumbnailW = 100;
my $maxThumbnailH = 100;
my $indexWidth = ($maxThumbnailW * $imagesPerLine) + ($hBorder *
($imagesPerLine - 1 + 2));
my $indexHeight = ($maxThumbnailH * $rows) + ($vBorder * ($rows - 1 + 2)
+ $titleBorder);
my $indexImage = new GD::Image($indexWidth,$indexHeight);
$indexImage->colorAllocate(255,255,255);
my $black = $indexImage->colorAllocate(0,0,0);
$indexImage->string(gdGiantFont,$hBorder,($titleBorder/2),$title,$black);
my $nextX = $hBorder;
my $nextY = $vBorder + $titleBorder;
foreach my $image (@images) {
my $thisX = $nextX;
my $thisY = $nextY;
my $fileName = $image;
$fileName =~ s/$dir\///;
my $thumbnail = &createThumbnail($maxThumbnailW,$maxThumbnailH,$image);
$indexImage->copy($thumbnail,$thisX,$thisY,0,0,$thumbnail->getBounds);
$indexImage->string(gdSmallFont,$thisX,($thisY - 15),$fileName,$black);
if ($nextX >= ($indexWidth - $hBorder - $maxThumbnailW)) {
$nextX = $hBorder;
$nextY += ($vBorder + $maxThumbnailH);
} else {
$nextX += ($hBorder + $maxThumbnailW);
}
}
open(IMAGE,">index.jpg")
|| die "Cannot open index.jpg for write: $!\n";
print IMAGE $indexImage->jpeg(100);
sub createThumbnail {
my ($maxwidth,$maxheight,$file) = @_;
my $srcimage = GD::Image->newFromJpeg($file);
my ($srcW,$srcH) = $srcimage->getBounds();
my $wdiff = $srcW - $maxwidth;
my $hdiff = $srcH - $maxheight;
my $newH; my $newW; my $aspect;
if ($wdiff > $hdiff) {
$newW = $srcW - $wdiff;
$aspect = ($newW/$srcW);
$newH = int($srcH * $aspect);
} else {
$newH = $srcH - $hdiff;
$aspect = ($newH/$srcH);
$newW = int($srcW * $aspect);
}
my $newimage = new GD::Image($newW,$newH);
$newimage->copyResized($srcimage,0,0,0,0,$newW,$newH,$srcW,$srcH);
return $newimage; }
[The colored lines above are each one line. They have been
split for formatting purposes.]
Conclusion
So in conclusion, we have used the GD library to create an index
image that consists of thumbnails of images in a given directory.
If you are working with many different images, you could modify
this script to automate the process and even link the thumbnails
to the full images. This might be helpful for providing images
for conferences, events, and meetings. I know that I've been able
to use the script quite a bit to organize my family pictures.
Create An Empty Index Image And Draw The Title - Page 23
Weaving Magic With Regular Expressions
Tracking FedEx and UPS Packages Online - Page 25
|