图像元数据信息EXIF和XMP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像元数据信息EXIF和XMP相关的知识,希望对你有一定的参考价值。

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;", " ", $xmp_item);
  59. }
  60. return $xmp_parsed;
  61. }

以上是关于图像元数据信息EXIF和XMP的主要内容,如果未能解决你的问题,请参考以下文章

php 对 WEBP 图像元数据的支持

Android中读取图片EXIF元数据之metadata-extractor的使用

错误地将 EXIF 元数据写入图像

如何在lua中获取webp图像EXIF元数据?

如何使用 IPTC/EXIF 元数据对照片进行分类?

Exif.js读取图像的元数据