This afternoon I started a project that I’d been meaning to do for a while now, writing my own image gallery script. I have done similar systems before (see my PhotoLounge, for instance), but I had an idea recently for something a bit more like the way Google Images displays its images. I’ve actually almost finished it now, save for a bit more refining and some aesthetic improvements, but the basic core of the system is there. The code will now display all jpg images in a specified folder, creating thumbnails if they are not already present.
The thumbnails generation really is the clever bit. First of all the script goes through the whole of the folder of images you give it, and for each one looks to see if there as a thumbnail for it already there. If there isn’t, it creates one. This is done using the GD library in PHP, first getting the original image size with imagesx() and imagesy(), scaling appropriately (maximum thumbnail size of 120px, either in width or height depending on aspect ratio), creating a resampled image using imagecopyresampled() and saving with imagejpeg() (having already checked to make sure the thumbs folder exists, and creating it if it doesn’t).
For those for whom all of this is a foreign language, I apologise. You can, however, see a demonstration of this code so you can see for yourself what is going on – there’s the main gallery script, which generates a basic output, and also a container page that calls the gallery script and formats it nicely with some neat CSS. Next on the to-do list will be to refine the formatting slightly, add in some additional information about the image, and test the whole thing in some more browsers to make sure it’s all working as it should.
Whether I’ll use this on my PhotoLounge remains to be seen, as I quite like it as it is for now. But I may end up pulling in the script for various clients of mine who want galleries on their web sites, as this will be an easy and effective way of doing it. No complicated setup, no database to worry about, just upload some photos and the script will handle the rest. It would even be possible to generate mid-res versions too if there was a need for it, so you would click on the thumbnail to get a bigger preview, then on something else to download the whole thing in all its glory.