Sometimes you make a promise to yourself for all the right reasons, but it turns out to be waaaay more painful than you thought it would be. Adding metadata to my all my photos has joined “stop biting my fingernails” and “giving up chocolate for July” and as something I’ve started with the best of intentions but only continued through sheer bloody-mindedness.
I want to live in a world where every photo has a descriptive caption and contains location information (latitude/longitude) along with the camera settings used (lens, zoom, aperture, ISO) and a way to contact the photographer. Information posted next to a photo quickly gets lost when it is inevitably re-posted to social media, and trying to track down the original using reverse image search is not how things should work. Metadata embedded into the photo file is the obvious solution, but this seems to be a less common practice than it should be. So anyway, I am making a commitment to put metadata on all of the photoshoot galleries on this website.
Metadata on twopotscreamer.com
This table is automatically updated when I upload new photos. How am I doing?
Cannot find gallery basedirThe IPTC website also has a tool you can use to view metadata for individual photos.
Metadata Fields in Lightroom
So my note to myself is to:
- Set basic metadata during import (copyright, creator, geolocation, and any keywords that apply to all of the photos).
- In the Library module, use the painter tool to bulk add any missing metadata.
- Hand-edit metadata on individual photos where needed.
- Add all photos to be published to a special “web” collection, give them a custom sort order, double-check the metadata and export with a descriptive filename.
As an added incentive to add creator metadata, Google Images has an “image credits” link that displays the XMP credit line field along with the XMP copyright notice. It is unclear whether Google Images does anything with the caption or keywords metadata fields though.
Photo metadata in PHP
I thought that extracting metadata from images with PHP would be fairly simple, but it was more complicated than I expected. Metadata can be:
- EXIF: extracted using exif_read_data. Make sure you nominate the correct section (EXIF/IFD0, etc. or just use “ANY_TAG” to get it all).
- IPTC IIM (Information Interchange Model): extracted using get_image_size (huh!!?!), then iptcparse.
- XMP (eXtensible Metadata Platform): XML embedded in the image file. A nightmare to process using the SimpleXML library.
The EXIF and IPTC stuff is clunky and a bit weird, but fairly simple. I gave up on extracting XMP values the “correct” way (using XML functions), and ended up using a regex. There is a comment from a 2011 Stack Overflow post that says “with namespaces SimpleXML causes hair loss.” I couldn’t agree more. If you want to see if you can read XMP data using simplexml_load_string and successfully extract values, here is a text file containing XMP data taken from one of my photos. Let me know if you succeed.
// Read EXIF metadata. Note: when extracting individual values, an empty value is returned if the key does not exist. $exif = exif_read_data($file, "ANY_TAG", true); $metadata_focallength = str_replace("/1", "mm", $exif["EXIF"]["FocalLength"]); // "24/1" becomes "24mm" $metadata_aperture = $exif["COMPUTED"]["ApertureFNumber"]; $metadata_shutterspeed = $exif["EXIF"]["ExposureTime"]; $metadata_iso = $exif["EXIF"]["ISOSpeedRatings"]; $metadata_camera = $exif["IFD0"]["Model"]; $metadata_lens = $exif["EXIF"]["UndefinedTag:0xA434"]; // seriously? // Read IPTC metadata // Not all the metadata is visible via exif_read_data, so it is clunkily read using getimagesize(!!!) getimagesize($image, $extra_info); $iptc = iptcparse($extra_info["APP13"]); $title_tag = $iptc['2#005'][0]; $tags = $iptc['2#025']; // Technically you should use an XML library to extract values from XMP metadata, // but can save frustration by using regular expressions instead. $content = file_get_contents($file); // Note: this loads the entire file into memory, so may have problems with big files. $xmp_data_start = strpos($content, '<x:xmpmeta'); $xmp_data_end = strpos($content, '</x:xmpmeta>'); // 12 chars $xmp_length = $xmp_data_end - $xmp_data_start; $xmp_data = substr($content, $xmp_data_start, $xmp_length + 12); preg_match('/Iptc4xmpCore:CiUrlWork="(.*)"\/>/', $xmp_data, $matches); // the creator URL is now in $matches[1]
I hope you got something out of this post (and that I didn’t totally waste my time here). If you are a photographer too, please consider adding metadata to all the photos you publish.
Leave a reply