 | |  |
 |

terrill
User
Jul 28, 2002, 6:26 PM
Post #1 of 9
(4829 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 --
"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
(This post was edited by terrill on Jul 29, 2002, 10:05 AM)
|
|
|  |
 |

dlo_itools
Staff

Jul 29, 2002, 10:29 AM
Post #2 of 9
(4811 views)
Shortcut
|
|
Re: [terrill] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Hi Terrill, that was a cool script. I like automation too... Just a general note, this does make the server work a little harder to regenerate the thumbnails each time. For pages with many thumbnails on a fully-loaded server, the CPU will be runnin' a little warmer... /Dave Lo
|
|
|  |
 |

terrill
User
Jul 29, 2002, 11:18 AM
Post #3 of 9
(4805 views)
Shortcut
|
|
Re: [dlo] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Snicker! [ Bandwidth | more work for me ] Pick one! Yeah, it does increase the overhead of your server a bit. But, viewing most web sites these days with all the ActiveX, Flash, Quicktime, WAV files, images galore, un-optimized HTML and other bandwidth-stealing/server-working stuff on 'em, I don't think the overhead of a few auto-generated thumbnails will be noticed. You'd think everyone has RoadRunner. One other benefit the technique offers is: disk space reduction. For those with limited disk space or a shortage of file-count-allotment offered by their Hosting service, one less file of 5K might mean more to 'em than a few milliseconds of image generation time. And since the "local" digestion of the original image shouldn't count as bits pushed out the server to the client, again the overhead might be ignored by the needy. Take if ya want it. At least, study/copy it and put it back in the dark recesses of your mind. Ninety-percent of the battle is knowing I can or can not do something. Nine-percent is finding someone else's code that almost does it. One-percent is modifying that code to do what you need. Normally, this process is followed as "read the online help, copy the example, and tweak it!" I've been programming since 1973. Not a lot of "original" code has come from me over that time. The program in question here was a modification of the examples provided by Lincoln Stein (author of GD.pm), and things I've built upon it over time. Enjoy!!! -- Terrill --
"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
|
|
|  |
 |

Colossus
New User
Jul 27, 2003, 10:20 PM
Post #5 of 9
(4210 views)
Shortcut
|
|
Re: [terrill] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Couldn't ImageMagick or NetPBM be used? Neither of these require root access to install.. I'm currently after a program such as Article Manager, and the only major thing I really need which Article Manager doesn't seem to provide is easy image management (mainly the ability to automatically thumbnail an uploaded image). If you've seen the photo gallery program "gallery", the upload/focus/automatic thumbnail functionality is the sort of thing I'm after. Perhaps you could create an image gallery program that could be sold seperately, but also make it easy to integrate to Article Manager :)
|
|
|  |
 |

Donna
Staff
/ Moderator

Jul 28, 2003, 12:08 PM
Post #6 of 9
(4205 views)
Shortcut
|
|
Re: [Colossus] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Hi Colossus! Thanks for your post. :) We do currently have thumbnailing available for Realty/Auto Manager with ImageMagick or GD... and it's definitely on the requested feature list for Article Manager as well. However, while Article Manager doesn't currently automatically thumbnail an image, you do have the ability to create a thumbnail that would link to a larger image. You could, for example, do something like this: 1. Create the thumbnail on your home computer -- I use some image batch software for creating a load of them at once. 2. Upload both the thumbnail and the large version. 3. When entering your article, put something like this:
<a href="***image1:url***">***image2***</a> ...this is assuming that you uploaded the fullsized version in "image1" and the thumbnail in "image2". You can, of course, muck about with it further in opening a new window for the new image, or even integrating some nifty javascript popup code. Let me know if you have any questions or concerns. Donna
Hire me! Save time by getting our experts to help with your project. Template changes, advanced features, full integration, whatever you need. Whether you need one hour or fifty, get it done fast with Priority Consulting.
|
|
|  |
 |

Colossus
New User
Jul 28, 2003, 3:31 PM
Post #7 of 9
(4201 views)
Shortcut
|
|
Re: [Donna] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Hi Donna, I've setup a demo system to see whether or not Article Manager suits my needs in other areas. I tried your code, however I seem to have the problem with Article manager placing the link to the larger picture outside of the table which the thumbnail resides in. Here's the example code:
<font class="arttext"> <a href="largepic"><table border=0 cellspacing=2 cellpadding=0 width=180 align=right> <tr><td><img src="smallthumb" height="87" width="180" border=1></td></tr> </table> </a> </font> The result of this appears to be that when I mouseover the thumbnail image, the status bar tells me what image the thumb is linked to, but there is no hyperlink so I can't actually click the smaller image to bring up the larger one. I'm currently using IE6 SP1.
|
|
|  |
 |

Donna
Staff
/ Moderator

Jul 28, 2003, 3:50 PM
Post #8 of 9
(4200 views)
Shortcut
|
|
Re: [Colossus] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Hi Colossus, Good to hear from you. :) The main problem with the code snippet you gave me is that it's invalid html. The result you're seeing is exactly what I would expect to see -- a link needs to be inside the table tag, you can't link an entire table like that. The table is being generated by the templatecell in the Article template. What you need to do is just remove the table information, and that should insert the html properly. So, open up the templates/article/default.html template, and find the following:
<!-- templatecell : img_caption --> <table border=0 cellspacing=2 cellpadding=0 width=$img_width$ align=$img_align$> <tr><td><img src="$img_url$" height="$img_height$" width="$img_width$" border=1></td></tr> <tr><td><font class="text10">$img_caption$</font></td></tr> </table> <!-- /templatecell : img_caption --> <!-- templatecell : img_nocaption --> <table border=0 cellspacing=2 cellpadding=0 width=$img_width$ align=$img_align$> <tr><td><img src="$img_url$" height="$img_height$" width="$img_width$" border=1></td></tr> </table> <!-- /templatecell : img_nocaption --> Now remove all of the table data so it's more along the lines of:
<!-- templatecell : img_caption --> <img src="$img_url$" height="$img_height$" width="$img_width$" border=1 align="$img_align$> <!-- /templatecell : img_caption --> <!-- templatecell : img_nocaption --> <img src="$img_url$" height="$img_height$" width="$img_width$" border=1 align="$img_align$"> <!-- /templatecell : img_nocaption --> Let me know if that works for you. :) Donna
Hire me! Save time by getting our experts to help with your project. Template changes, advanced features, full integration, whatever you need. Whether you need one hour or fifty, get it done fast with Priority Consulting.
(This post was edited by Donna on Jul 28, 2003, 3:54 PM)
|
|
|  |
 |

Colossus
New User
Jul 30, 2003, 3:23 PM
Post #9 of 9
(4167 views)
Shortcut
|
|
Re: [Donna] Automatic Thumbnails for Images
[In reply to]
|
Can't Post
|
|
Hi Donna, I haven't yet purchased AM so hadn't had the opportunity to look at the source code. I can see now why AM adds the link outside of a table. Having had a chance to think about it, the full sized images could be shown seperately in a dedicated image gallery anyway. I'll continue to experiment with other aspects of the program and we'll see how things pan out. Thanks
|
|
|  |
|