PHP: Faster thumbnail gallery
Community Forums/General Help/PHP: Faster thumbnail gallery
| ||
Hey, the thumbnail gallery I use is pretty slow, because all thumbnails are generated on the fly.. Does anyone know of a faster method? This is a call to show a photogallery on any given page: $sgal is the name of the folder of pictures to show. showgallery.php This one loops through each file in the folder that is an image and gives the file name to thumb.php, which spits out a thumbnail. thumb.php This one uses GDI to output a thumbnail. What can I do that would give me the least editing possible? |
| ||
I was thinking of something like this: *Cycle through each image in the gallery *If the image has a thumbnail counterpart in a corresponding folder, output that thumbnail *if the image does not have a thumbnail counterpart, then create that counterpart, and output that thumbnail. Something like this I would expect to have minimal changes, but I'm not 100% sure how to go about the thumbnail file creating and checking. How do you save the created thumbnail to a file rather than outputting to the browser? |
| ||
The way you can create thumbnails that will have the least affect on performance is to use something like a cron job. These run at regular intervals, not when you click on the image in the browser. This could be done using ImageMagick and a simple shell script on a Unix/Linux/FreeBSD based web server. Basically for each image: - Check if there is a thumbnail fail. - Check if the image is newer than the thumbnail. - If no thumbnail or thumbnail is newer, make a new one (convert command). The only downside is that if you dump in lots of new images, there won't be any thumbnails for them until your cron job runs, or you run it manually. You could have a placeholder thumbnail instead. The same could, of course, be done in your php code, and have it check for thumbnails and the date of the files vs. thumb, every time you view the page. This is pretty quick. To save the image to disk from php do: imagejpeg($image, 'filename', 75); See http://php.net/manual/en/function.imagejpeg.php |