本文介绍了如何使用PHP从JPG读取XMP数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP内置了对读取EXIF和IPTC元数据的支持,但是我找不到任何读取XMP的方法吗?

PHP has built in support for reading EXIF and IPTC metadata, but I can't find any way to read XMP?

推荐答案

XMP数据实际上是嵌入到图像文件中的,因此可以使用PHP的字符串函数从图像文件本身中提取它.

XMP data is literally embedded into the image file so can extract it with PHP's string-functions from the image file itself.

以下内容演示了此过程(我正在使用 SimpleXML ,但使用其他所有XML API甚至简单而聪明的字符串解析都可以为您提供相等的结果):

The following demonstrates this procedure (I'm using SimpleXML but every other XML API or even simple and clever string parsing may give you equal results):

$content = file_get_contents($image);
$xmp_data_start = strpos($content, '<x:xmpmeta');
$xmp_data_end   = strpos($content, '</x:xmpmeta>');
$xmp_length     = $xmp_data_end - $xmp_data_start;
$xmp_data       = substr($content, $xmp_data_start, $xmp_length + 12);
$xmp            = simplexml_load_string($xmp_data);

只有两句话:

  • XMP大量使用XML名称空间,因此在使用某些XML工具解析XMP数据时,您必须注意这一点.
  • 考虑到图像文件的可能大小,您可能无法使用 file_get_contents() ,因为此功能会将整个图像加载到内存中.使用 fopen() 打开文件流资源并检查数据块中的密钥-序列<x:xmpmeta</x:xmpmeta>将显着减少内存占用.
  • XMP makes heavy use of XML namespaces, so you'll have to keep an eye on that when parsing the XMP data with some XML tools.
  • considering the possible size of image files, you'll perhaps not be able to use file_get_contents() as this function loads the whole image into memory. Using fopen() to open a file stream resource and checking chunks of data for the key-sequences <x:xmpmeta and </x:xmpmeta> will significantly reduce the memory footprint.

这篇关于如何使用PHP从JPG读取XMP数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 11:12