Recently I have been tasked with automatically compiling, tagging and indexing images added to an application via FTP. The images had been tagged using Adobe Bridge or Photoshop, anyone with knowledge of Adobe products will be familiar with tagging images. For those who aren’t, you can add descriptions, titles, ratings, keywords and copyrights (to name a few) to the core of an image using Bridge or Photoshop. My challenge was extracting this information at the other end. As the user would be uploading 1000s of images, there was absolutely no way they would want to tag each image after waiting for it to upload via a web form. After a few cron scripts dumping errors and FTP permission issues I managed to configure a system that would listen in a drop folder on one of the servers and then chmod and move the file to a web friendly directory for processing. This is where the interesting stuff begins.

As if, exif!

One of two core PHP functions used to pull this information out is exif, which wasn’t the easiest thing to find as googling “PHP get image keywords” was surprisingly unsuccessful. PHP’s explanation of exif is as follows:

With the exif extension you are able to work with image meta data. For example, you may use exif functions to read meta data of pictures taken from digital cameras by working with information stored in the headers of the JPEG and TIFF images.

Exif may be quite a large library, but all I used was exif_read_data:

$path = '/path/to/image/img.jpg';

$exif = exif_read_data($path);

//Lets pull out some information just as an example
$description = $exif['ImageDescription'];
$filesize    = $exif['FileSize'];
$xresolution = $exif['XResolution'];
$yresolution = $exif['YResolution'];

I was fairly happy with the amount of information that EXIF could give me about an image, for more information about other values, see the documentation. But I am still missing quite a lot of the required information. After a lot more googling, I managed to find the poorly documented reference parameter in the getimagesize() function. Once getimagesize returns the data array, it will be meaningless unless we iptcparse it.

$path = '/path/to/image/img.jpg';

$imagesize = array();
getimagesize($path, $imagesize);
//Lets pull out some more information just as an example
$tags = array();
$tags = $imagesize['2#025'];
$title = '';
$title = $imagesize['2#005'][0];

This may only be a simple example but, why would I do all of the work for you?! Happy var_dump()ing!