Adobe XMP with an Hierarchical Subject Array in PHP

This morning I had a bit of a challenge parsing Adobe XMP information for images on Underwater Focus. The Adobe XMP is too complex for SimpleXML, and anyway, I only needed a few values — one of them, the LightRoom hierarchicalSubject keywords, is the reason I’m sharing some of the code I wrote.

Using regular expressions to get at single values is quick and easy, but I wanted to create arrays for rdf:li values, and split each lr:hierarchicalSubject keyword into an additional second-dimension array.

A print_r() of the returned $xmp variable looks like this:

And here’s how I used the $xmp[‘hierarchs’] two-dimensional array to print a list of keywords, one hierarchical keyword set per line:

You can see a finished hierarchical keyword example on photograph pages from Underwater Focus.

Update : This PHP code has been improved over time and is the basis for the Adobe XMP for WP WordPress plugin. The Adobe XMP for WP plugin reads image files progressively (small chunks at a time) to extract the embedded XMP meta data, instead of reading the whole file into memory (as many other image management plugins do). The extracted XMP data is also cached on disk to improve performance and is refreshed only if / when the original image is modified. You can use the plugin in one of two ways; calling a method from the $adobeXMP global class object in your template(s), or using an [xmp] shortcode in your Posts or Pages.

Find this content useful? Share it with your friends!