> Why create thumnails? You can resize the images on > the fly when needed. This would be too expensive. I need to store the thumbnails in the database. So, I need to resize the image at the same time as I store it. I can do this and save the thumbnail in the file system, but cannot save the thumbnail in the database.
Kevin,
I'm working on a web based photo gallery site which stores images in a MySQL database so you and I are working on something pretty similar. Like you I'm storing both the thumbnail (150x150) and a full sized image (768x512 or 512x768 depending on image orientation) in the database. You you could only store the full sized images in a database and resize them to thumbnails when they are needed but when you have 25 or more thumbnails to display on the same page resizing them on the fly pegs the CPU at 100% and if you have any other sites running on that same box they will experience a slow down too so that is why I opted to store the thumbnails in the database also. Disk space is very cheap and the thumbnails don't take too much space. Worse case scenario you can add another drive to the machine you are hosting the site on and presto, you have more disk space.
In my web application I create the thumbnail and the full sized image all in one pass when a user uploads an image though a web page. Creating both the thumbnail and the full size images doesn't take that much CPU time. If I load in an image from my 2 megapixel digital camera (1600x1200) it will take 0.3 seconds on my 1.8GHz P4 to resize the image to a thumbnail and to a full sized image to be displayed in my web gallery.
I don't know how to create an image in memory and write that to the database so I'm writing both the thumbnail and the full sized image out to my /tmp directory and then re-reading them into the database. At first I thought it might seem to be a bit inefficient but with this method I'm getting the 0.3s time to read in the original file, create a thumbnail, write it out to disk, read it into the database, create a full sized image, write it out to disk, read it into the database so I'm pretty happy with it. The code goes something like this:
---
// read in the image file from the disk that needs to be resized and uploaded
$image_original = ImageCreateFromJpeg($path_to_image_file_on_disk);
// allocate memory for the thumbnail image
$image_thumbnail = ImageCreateTrueColor($thumbnail_width, $thumbnail_height);
// copy and resize the original image into the thumbnail memory we have allocated
ImageCopyResized($image_thumbnail, $image_original, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, imagesx($image_original), imagesy($image_original));
// save the newly created thumbnail to disk
ImageJpeg($image_thumbnail, $path_to_save_thumbnail_image, 100);
// open up the thumbnail file for reading and then encode it with MIME Base64
$thumbnail_contents = base64_encode(fread(fopen($path_to_read_thumbnail_from, "rb"), filesize($path_to_read_thumbnail_from)));
// delete the thumbnail file from the disk
unlink($path_to_read_thumbnail_from);
// deallocate memory used to create our original and thumbnail images
ImageDestory($image_original);
ImageDestory($image_thumbnail);
---
You do the same thing for the full sized image you want to store in the database as well but you have to make sure you get the image's orientation right otherwise your resize will give you a hard time. When it's time to insert the images into the database for the thumbnail you will insert $thumbnail_contents and whatever else variable you chose for the full sized image. One thing to watch out for is that when you read the images from the database you want to use base64_decode() on them. Hope this works out for you. If you need more help give me a shout.
-Pete
-- perl -e 'print pack("H*", "70766572746573406E79632E72722E636F6D0A")' |
signature.asc
Description: This is a digitally signed message part