# HG changeset patch # User Matti Hamalainen # Date 1558032359 -10800 # Node ID 54a54921426c17b7e8a5458e5623234c9ac06921 # Parent 6770ef8b3575dc86ac373031b12a50d738fb6a6d Add functions for extracting XMP information from files and parsing the XMP to get out some relevant fields of data not present in EXIF tags. diff -r 6770ef8b3575 -r 54a54921426c mgtool.php --- a/mgtool.php Thu May 16 17:59:18 2019 +0300 +++ b/mgtool.php Thu May 16 21:45:59 2019 +0300 @@ -27,6 +27,7 @@ [ MG_DATE, "datetime" , "DateTimeOriginal" ], [ MG_DATE, "datetime" , "DateTimeDigitized" ], [ MG_INT, "filesize" , "FileSize" ], + [ MG_STR, "keywords" , "keywords" ], ]; @@ -210,6 +211,125 @@ // +// Read and parse XMP data from given file +// .. a horrible hack. +// +function mgReadXMPData($filename, $xmpBlockSize = 1024*64) +{ + if (($fh = @fopen($filename, 'rb')) === FALSE) + return FALSE; + + $xmpStartTag = "/" + ]; + + $xmpReplacements = + [ + "", + "\/", + "", + ]; + + $xmpStr = preg_replace($xmpPatterns, $xmpReplacements, $xmpStr); + + // Parse XML to a SimpleXMLElement structure + if (($xmpOb = @simplexml_load_string($xmpStr)) === FALSE) + return FALSE; + + // Process structure to simple flat array of data + // for the desired elements only + $xmpData = []; + + //if (($tmp = $xmpOb->xpath("RDF/Description/description/li")) !== FALSE) + // $xmpData["description"] = (string) $xe[0]; + + if (($xres = $xmpOb->xpath("RDF/Description/subject/li")) !== FALSE) + { + $xmpData["keywords"] = array_map(function($xkw) { return (string) $xkw; }, $xres); + } + + return $xmpData; +} + + +// // Converts one value (mainly from EXIF tag information) // by doing explicit type casting and special conversions. // @@ -723,6 +843,16 @@ mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) $updFlags |= GUPD_CAPTION; + // Check for XMP info + // TODO XXX: Support XMP sidecar files + if (($updFlags & GUPD_EXIF_INFO) && + ($xmpStr = mgReadXMPData($efilename)) !== FALSE && + ($xmp = mgParseXMPData($xmpStr)) !== FALSE) + { + foreach ($galExifConversions as $conv) + mgCopyEntryData($edata, $xmp, $conv[0], $conv[1], $conv[2]); + } + // Check for EXIF info if (($updFlags & GUPD_EXIF_INFO) && ($exif = @exif_read_data($efilename)) !== FALSE)