Automatically organise your digital photography by taking advantage of your cameras built-in metadata and PHP's EXIF functions.
If you have a digital camera, you probably also have a digital photo gallery of memorable moments. And if you have a digital photo gallery, you're almost sure to have run into a very common problem: organising your photos so that they can be easily searched and indexed.
Now, you might not know this, but most digital cameras automatically embed descriptive metadata in the headers of the images they create. These headers, called EXchangeable Image File (EXIF) headers, contain information on the camera make and model, the time and date the photo was taken, the technical specifications of the photo (shutter speed, aperture and so on) and a thumbnail of the image. Additionally, many image editors allow you to supplement these automatically generated headers with descriptive text of your own -- for example, "Sally's first football game" or "Getting drunk in Malta".
I'm sure you can see where I'm going with this. With a little bit of imagination and creative thinking, it's possible to use the headers generated by your camera (and, if you have the time and motivation, further supplemented by you) to automatically organise and describe your photo collection. This article will show you how, using PHP's EXIF functions.
Step 1: Make sure your PHP build supports EXIF
In order to read EXIF headers, your PHP build must include support for the EXIF module. You can check whether this support is enabled, by creating a PHP script containing the code shown in Listing A.
Listing A
<?php
phpinfo();
?>
View the output of this script in your Web browser, and review the list of extensions to see if EXIF is included. If it is, move to the next step. If not, you'll need to activate PHP's EXIF functions, either by un-commenting the extension line in php.ini (Windows) or recompiling your PHP build with the --enable-exif argument (UNIX). More information on how to do this is available at the PHP Web site.
Step 2: Move your photos into a single directory
Next, collect all your photos into a single directory under the Web server document root. This is also a good time to add your own descriptive comments to each image (although this is not essential). A number of good shareware and freeware tools are available to help you do this; take a look at Exifer for Windows or RoboPhoto.
Step 3: Write code to read photo headers and comments
The final step is to write the PHP scripts that will extract EXIF data from your images and automatically generate a Web page with thumbnails, technical information and links to larger versions of each image. There are two scripts here: the first one, gallery.php, (See Listing B) looks for photos and extracts EXIF headers from them, while the second one, thumbnail.php, (See Listing C) is responsible for extracting the thumbnail image from each photo.
Listing B