php and EXIF data

A colleague at work gave me the idea of storing metadata for each photo on my brother’s site inside its EXIF data. I liked this idea as I originally thought I would need another text file on disk, which described the photos. Tying data to an object by adding it to the object itself is also much more robust.

 

The EXIF worked out pretty well, as I could read the data out using simple code like this on the backend:

$exif = exif_read_data("$main_pic",'IFD0');
echo $exif['COMMENT']['0'];
?>


But this wouldn’t work with some photos which already had metadata of various types – no data was returned by the php call. I usually set the data using the tools available with KDE, but it seems there are many types of metadata headers in JPEG photos, and some interfere with this code. I ended up using jhead, which is a handy command-line tool for manipulating EXIF data. I used it to strip all metadata from the troublesome photos before re-inserting the EXIF data I wanted. Then it all worked fine.

$ jhead -mkexif cabinet.jpg
$ jhead -ce cabinet.jpg


I suspect I may have been able to tweak the php code to deal with this issue, but in any event I like the idea of having complete control over the metadata.