
terrill
User
Jul 28, 2002, 6:26 PM
Post #1 of 9
(7725 views)
Shortcut
|
|
Automatic Thumbnails for Images
|
Can't Post
|
|
Ya may have notcied, this is my second post with the word "Auto" in the subject. I hate working... Find below, a Perl program which generates a thumbnail image of any JPEG at your desired width. Why: * You have numerous images on your site. You'd like a thumbnail for your story on the frontpage, and the full size image in your actual story. * Normally, you'd have to open the fullsize image in your favorite image software (uh, that'd be Photo Impact, right?) and reduce the image to a smaller size for the thumbnail. Then, upload the smaller image, and place the IMG SRC in your lead. * You now have, count 'em, two images to maintain. Darn - doubling my work! I hate it when that happens!!! Using the Perl code below, in your xhtml where you'd like your thumbnail, you instead write something like: <img src="mkt.cgi?w=100&i=relative/path/to/somebigimage.jpg"> where: mkt.cgi is the code below (you can name it anything you like. Mine stands for "MaKe Thumbnail") w=NNN desired WIDTH of the thumbnail in PIXELS i=relativePath to your fullsized image (relative from your CGI script). The program takes your desired width (100 pixels in my example), opens the original JPEG image and gets it's bounds (width and height), computes a factor, and produces your thumbnail on the fly. Humph. No second image to maintain. And, if you find a better image, just write it over the top of the original image, and thar-ya-be... all done. No xhtml to change, no new thumbnail to re-make, more time to play Tetris... all in one fell swoop. Who said being lazy meant getting less work done, easily??? Here's the Perl program. The modules GD and CGI are required. If you get an error, contact your Hosting people and ask 'em to install GD. CGI should already be there. And, enjoy being lazy. Just tell people you're "efficient." Oh, GD understands other image types besides JPEGs, so feel free to modify it to decide dynamically if you want a new GIF, or BMP, or PNG or whatever. You didn't think I was going to do it for you, did you? ===== PROGRAM STARTS HERE ==== #!/usr/bin/perl ### LITTLE OR NO ERROR CHECKING, Because like most programmers, ### our code always be perfect... so we expect our users to be, too. use GD; # graphics lib (not available everywhere!) use CGI; $query = CGI::new(); my $r = $query->param("w"); # desired width for new image my $imgIn = $query->param("i"); # relative path to image # create internal copy of old image for our use my $oldImage = newFromJpeg GD::Image($imgIn) or die "Can't open old image: $imgIn - $!"; # get old image size (my $oldW, my $oldH) = $oldImage->getBounds(); # compute desired image size, and make a new image object $r = $oldW / $r; # compute factor (ratio) # use factor to maintain apsect ratio my $imgW = $oldW / $r; # compute new width my $imgH = $oldH / $r; # compute new height # create new image at desired width/height # makes the image BIGGER or smaller, whichever! my $newImage = new GD::Image($imgW, $imgH); # copy old image to new resized image $newImage->copyResized($oldImage, 0,0,0,0, $imgW,$imgH,$oldW,$oldH); # send new image to the browser print "Content-type: image/jpeg\n\n"; # make standard output binary mode and write the image data binmode STDOUT; print $newImage->jpeg; # bye-bye! We're done. I want a raise... -- Terrill -- [quote] "Evaporating expectations of quality: 1980's paradigm: If it's worth implementing once, it's worth implementing twice. 1990's paradigm: Ship the prototype! 2000's paradigm: Ship the idea!" ---Larry Rosler: http://www.perl.com/pub/a/2000/06/rosler.html [/quote]
(This post was edited by terrill on Jul 29, 2002, 10:05 AM)
|