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.

6 thoughts on “Adobe XMP with an Hierarchical Subject Array in PHP

  1. Bonsoir,
    chez moi, les “simples” balises ne s’affichent pas :
    [“email”]=> string(0) “” [“created”]=> string(0) “” [“modified”]=> string(0) “” [“state”]=> string(0) “” [“country”]=> string(0) “” [“owner”]=> string(0) ”

    J’ai repris le code en faisant un copier-coller : même chose.
    Un problème dans l’écriture des regex ? Je cherche depuis un moment, je n’ai rien trouvé.

    C’est vrai que pour quelques données, c’est apparemment plus simple que simpleXML et les children(‘namespace’), qui n’ont pas l’air de fonctionner du tout.
    OK pour le français ?

    • Perhaps you have an incomplete XML fragment? You should have both a start and end x:xmpmeta tag. I’ve been using this code unchanged (though I’ve added additional fields over time) on many different sites and images without any problem.

      Have a look at the PHP code for a more recent / complete version of this code. You’ll probably be interested in both the get_xmp_array() and get_xmp_raw() methods. ;-)


      • Thank you.
        I just discovered this code was written for WP !

        Here is the code I found to use in a php sript :
        ‘created’ => ‘.*’,
        ‘modified’ => ‘.*’,
        ‘state’ => ‘.*’,
        ‘country’ => ‘.*’,
        ‘ImageNumber’ => ‘.+’,


        // get a single text string
        $xmp[$key] = preg_match( “/$regex/is”, $xml, $match ) ? $match[0] : ”;

        It works, but I had to verify something with exif fields : someones are vey strange.

        And i spent time to have o look on your pictures !
        Here, I try to get some pictures from dolphins, (with a computer). For the moment, I didnt’ get the same quality !
        Good night from France.

        • Ohooo
          the code is not the good one !
          I try to paste once again :

          // ‘created’ => ‘.*’,
          // ‘modified’ => ‘.*’,
          // ‘state’ => ‘.*’,
          // ‘country’ => ‘.*’,
          // ‘ImageNumber’ => ‘.+’,

Comments are closed.