PDA

View Full Version : generating real thumbnails on the fly


matthew tucker
11-28-2003, 04:41 AM
Has anyone worked out how to generate a real resized image thumbnail on the fly using PHP and the GD library? I can't find a hack for it.

In other words, not just displaying the image in a resized <img> tage, but displaying a real re-sized image?

I can do it from an image FILE read directly from an images folder, but I can't work out how to pass the data from $attachment(filedata) to the ImageCreateFromJPEG ($imgfile) function. Just replacing $imgfile with $attachment(filedata) doesn't work of course.

The top image in the left hand column at the following website is done from a file in a folder.
http://www.screeneditors.com/forums/index.php

the other images in the left hand column are just resized. Modem users complaining a bit ...

is there a better way than saving the imagedata in a temp file then processing it?

NTLDR
11-28-2003, 03:01 PM
You need to specify a filename and path, passing the data won't work. You can use the URL to the attachment and pass that:

imagecreatefromjpeg('hhtp://www.mysite.com/forums/attachment.php?attachmentid=2');

Something similar to the above should work.

matthew tucker
11-29-2003, 08:33 PM
Thank you Lee

But most of my images are in the MYSQL attachments database.

How can I do the above transformation on them *without* exporting them to disk as files??

I'm think about installing the "attachments as files" hack but I don't want to unless I really need to.

(Botley? I was brought up in Botley, Oxford! first time I've seen that name in 20 years!)

filburt1
11-29-2003, 08:37 PM
Thank you Lee

But most of my images are in the MYSQL attachments database.

How can I do the above transformation on them *without* exporting them to disk as files??

I'm think about installing the "attachments as files" hack but I don't want to unless I really need to.

(Botley? I was brought up in Botley, Oxford! first time I've seen that name in 20 years!)
Look at (IIRC) imagecreatefromstring: http://www.php.net/imagecreatefromstring

matthew tucker
11-30-2003, 08:13 AM
imagecreatefromstring() does it!

thanks for that pointer.

but it seems REALLY slow ... is this normal? I mean, it seems slower than just loading the whole image!

using basic code from PHP.net ...

header('Content-type: image/jpeg');
$data = $attachmentinfo[filedata];
$size = 150;
$src = imagecreatefromstring ($data);
$width = imagesx($src);
$height = imagesy($src);
$aspect_ratio = $height/$width;

if ($height <= $size) {
$new_w = $width;
$new_h = $height;
} else {
$new_h = $size;
$new_w = abs($new_h / $aspect_ratio);
}

$img = imagecreatetruecolor ($new_w,$new_h);

imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height);

// or imagecopyresized()

imagejpeg($img ); //,'', 90

matthew tucker
11-30-2003, 08:49 AM
OK yep, its normal. Just read up a bit more on PHP.net.

So ... I think from now on I'll be generating thumbs on upload and store in database...