Thursday, September 11, 2008

Embedding Geo Data into the Exif data for your photos

I've been geotagging my photos for a while now, but the way I've done it in the past meant the coordinate data was in a database with just a link to the photo's name on my web server, so I thought I'd do it a better way an put the data into the Exif within the photo itself.

This wasn't too difficult in the end, but it took me a lot of google searching to piece together the information, so hopefully this post will get a higher ranking and save someone else from getting all the parts.

First up, this is all done in php, so you need the PEL (PHP Exif Library), you can get this here: http://pel.sourceforge.net/ there are some pretty good examples with it, so anyone with PHP knowledge should be able to use it to write Exif data to images pretty quickly.

To read the data back out, I found the PHP built in "exif_read_data" to be pretty good (http://www.php.net/exif).

Anyway, the next hurdle came when I tried to jam Coordinate data into the Exif without really understanding what was going on, tried a lot of things, but finally found out that the Exif standard doesn't use decimal location info, instead using the hours, minutes, seconds notation. And secondly, the GPS parts of the Exif are within a sub tag. Anyway, seems that PEL used to contain a GPS example which was missing in the version I have (2.0.0), but you can get the file back from the SVN logs on sourceforge (http://pel.svn.sourceforge.net/viewvc/pel?view=rev&revision=509). Again the example is pretty well written as to be understandable to anyone with php experience and also contains a handy function to convert decimal coordinates into the format required for the Exif.

Hope this helps someone else in the same boat.