Posted By

frankyfish on 03/20/10


Tagged

image exif metadata Information xmp


Versions (?)

Image Metadata Information EXIF AND XMP


 / Published in: PHP
 

Currenty getImagInfo() also call getImageXMP() so you only need the first function to get your infos.

  1. function getImageInfo($img) {
  2. $ret = array("title"=>"","description"=>"");
  3. if(!file_exists($img)) return $ret;
  4. $size = getimagesize($img, $info);
  5. if(!isset($info['APP13'])) return $ret;
  6. $iptc = iptcparse($info['APP13']);
  7.  
  8. if(is_array($iptc["2#025"]) && count($iptc["2#025"])>0) $ret["tag"] = implode(", ", $iptc["2#025"]);
  9. if($iptc["2#005"][0] != null) $ret["title"] = $iptc["2#005"][0];
  10. if($iptc["2#080"][0] != null) $ret["author"] = $iptc["2#080"][0];
  11. if($iptc["2#085"][0] != null) $ret["credit"] = $iptc["2#085"][0];
  12. if($iptc["2#090"][0] != null) $ret["city"] = $iptc["2#090"][0];
  13. if($iptc["2#095"][0] != null) $ret["state"] = $iptc["2#095"][0];
  14. if($iptc["2#101"][0] != null) $ret["country"] = $iptc["2#101"][0];
  15. if($iptc["2#105"][0] != null) $ret["title"] = $iptc["2#105"][0];
  16. if($iptc["2#116"][0] != null) $ret["copyright"] = $iptc["2#116"][0];
  17. if($iptc["2#120"][0] != null) $ret["description"] = $iptc["2#120"][0];
  18. if($iptc["2#122"][0] != null) $ret["author"] = $iptc["2#122"][0];
  19.  
  20. $xmp = getImageXMP($img);
  21. foreach($xmp as $key => $value) {
  22. if($value != null && $value != "") $ret[$key] = $value;
  23. }
  24. return $ret;
  25. }
  26.  
  27. function getImageXMP($filename) {
  28. $file = fopen($filename, 'r');
  29. $source = fread($file, filesize($filename));
  30. $xmpdata_start = strpos($source,"<x:xmpmeta");
  31. $xmpdata_end = strpos($source,"</x:xmpmeta>");
  32. $xmplenght = $xmpdata_end-$xmpdata_start;
  33. $xmpdata = substr($source,$xmpdata_start,$xmplenght+12);
  34. fclose($file);
  35. $xmp_parsed = array();
  36. $regexps = array(
  37. array("name" => "copyright", "regexp" => "/<dc:rights>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:rights>/"),
  38. array("name" => "author", "regexp" => "/<dc:creator>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/dc:creator>/"),
  39. array("name" => "title", "regexp" => "/<dc:title>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:title>/"),
  40. array("name" => "description", "regexp" => "/<dc:description>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/dc:description>/"),
  41. array("name" => "camera model", "regexp" => "/tiff:Model=\"(.[^\"]+)\"/"),
  42. array("name" => "maker", "regexp" => "/tiff:Make=\"(.[^\"]+)\"/"),
  43. array("name" => "width", "regexp" => "/tiff:ImageWidth=\"(.[^\"]+)\"/"),
  44. array("name" => "height", "regexp" => "/tiff:ImageLength=\"(.[^\"]+)\"/"),
  45. array("name" => "exposure time", "regexp" => "/exif:ExposureTime=\"(.[^\"]+)\"/"),
  46. array("name" => "f number", "regexp" => "/exif:FNumber=\"(.[^\"]+)\"/"),
  47. array("name" => "iso", "regexp" => "/<exif:ISOSpeedRatings>\s*<rdf:Seq>\s*<rdf:li>(.+)<\/rdf:li>\s*<\/rdf:Seq>\s*<\/exif:ISOSpeedRatings>/"),
  48. array("name" => "focal lenght", "regexp" => "/exif:FocalLength=\"(.[^\"]+)\"/"),
  49. array("name" => "user comment", "regexp" => "/<exif:UserComment>\s*<rdf:Alt>\s*<rdf:li xml:lang=\"x-default\">(.+)<\/rdf:li>\s*<\/rdf:Alt>\s*<\/exif:UserComment>/"),
  50. array("name" => "datetime original", "regexp" => "/xmp:CreateDate=\"(.[^\"]+)\"/"),
  51. array("name" => "lens", "regexp" => "/aux:Lens=\"(.[^\"]+)\"/")
  52. );
  53. foreach ($regexps as $key => $k) {
  54. unset($r);
  55. preg_match ($k["regexp"], $xmpdata, $r);
  56. $xmp_item = @$r[1];
  57. if(in_array($k["name"], array("f number", "focal lenght"))) eval("\$xmp_item = ".$xmp_item.";");
  58. $xmp_parsed[$k["name"]] = str_replace("&#xA;", "\n", $xmp_item);
  59. }
  60. return $xmp_parsed;
  61. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Vordreller on March 20, 2010

I'm getting a parse error for getImageXMP:

Parse error: syntax error, unexpected ';' in C:\wamp\www\Tests\test.php(48) : eval()'d code on line 1

on $key = 9 and another one at $key = 11

Posted By: frankyfish on March 20, 2010

I just updated the code... everything should work fine.

Posted By: edwin on April 3, 2010

Unfortunately Parse error: syntax error, unexpected ';' in /var/www/exif.php(60) : eval()'d code on line 1

The only thing I added was a call to that function

You need to login to post a comment.