Web Hosting Talk







View Full Version : Most efficient thumbnailing method?


ub3r
10-22-2006, 09:14 PM
I'm considering a thumbnailing measure for my image hosting service, and was wondering what you programming forum people feel is the most efficient method of generating thumbnails.

Right now, I'm looking at a simple little imagemagick command:

convert upload.jpg -thumbnail 300 -fill darkblue -pointsize 15 -draw "text 200,210 'photos.cx'" upload-t.jpg

However, I can't help to think that there might be a method out there.

Features i'd like to have:

1) Resize based on aspect ratio, 300 pixels on the longest side
2) ability to embed some text, or possibly a logo in the bottom right corner
3) Optionally, the ability to change opacity in the source image, but leave embedded logo opacity at 100%

Xeentech
10-23-2006, 07:00 AM
The way I scale and keep aspect ratio is to take the size I want for the longest side and divide that by the longest side of the image, that is then the scale. I then multiply the scale by each dimention.. some code might make it easier to explain.

$scale = ($in{size} / $height) if ($height > $width);
$scale = ($in{size} / $width) if ($width > $height);
$scale = ($in{size} / $width) if ($width == $height);

$tn = GD::Image->new(($width * $scale), ($height * $scale)) or die "Can't make image\n";
$tn->saveAlpha(1);
$tn->copyResized($image,0,0,0,0,($width * $scale), ($height * $scale),$width,$height);


This code uses GD in Perl btw. It has functions to insert logos and mess with opacity etc.

astellar
10-23-2006, 08:12 PM
Here is image resize using PHP:

$max_thumbnail_size = 200;
$thmb_height = ($width > $height) ? ($height / $width) * $max_thumbnail_size : $max_thumbnail_size;
$thmb_width = ($width > $height) ? $max_thumbnail_size : ($width / $height) * $max_thumbnail_size;

$thmb = imagecreate($thmb_width, $thmb_height);
$gdpic_in = imagecreatefromjpeg($pic_file);
imagecopyresized($thmb, $gdpic_in, 0, 0, 0, 0, $thmb_width, $thmb_height, $width, $height);
I'm not sure you need exaclty PHP, so there is another image resize methods (http://look4docs.com/image+resize.shtml), so find your favorite.

CodyRo
10-24-2006, 07:18 PM
I would either use GD (with PHP personally) and either resize as the above post suggests, or just when you output the html just change the height / width.

(<img src="blah.jpg" width="250" height="250" /> etc)

Czaries
10-27-2006, 12:56 PM
I would go with GD since there are far more examples out there and it's much easier to find code for. You will want to create some sort of resize cache on your hard disk to save the thumbnail images so you're not regenerating them on the fly every time they are requested. Hard disk space is far cheaper than processing power. This method also has the advantage of the images getting cached on the user's computer so they are not downloaded each time either, saving you bandwidth.

CodyRo
10-27-2006, 02:59 PM
I agree with the above statement. What I usually do is just save them using imagejpeg() (http://us3.php.net/imagejpeg) to the "thumbs" directory, and the full image to "images" with the same filename. This way it's easy to link / etc.