使用Imagick / PHP从Exif数据中获取特定的键值



我使用PHP Imagick图像库,并希望存储从上传图像的EXIF数据的宽度和高度值。我可以循环遍历数据以列出所有值,但我不知道如何提取特定的宽度和高度。使用数组位置没有意义,因为显然图像都将具有不同的EXIF数据,并且这些值并不总是在相同的数组位置?

// new Imagick instance for uploaded files (the $temp variable relates to a foreach loop that saves the uploaded images in temporary memory)
$image = new Imagick($temp);
/* Get the EXIF information */
$exifArray = $image->getImageProperties();
/* Loop trough the EXIF properties */
foreach ($exifArray as $name => $property)
{
echo "{$name} => {$property}<br />n"; 
}

输出键/值,例如:

exif:FocalPlaneResolutionUnit => 3
exif:FocalPlaneXResolution => 49807360/32768
exif:FocalPlaneYResolution => 49807360/32768
exif:ImageLength => 876
exif:ImageWidth => 1313
exif:ISOSpeedRatings => 100
exif:Make => Canon
exif:MaxApertureValue => 3/1
exif:MeteringMode => 5
exif:Model => Canon EOS 6D
exif:Orientation => 1

如何针对特定的属性值,例如ImageWidth。Imagick的文档不是特别有用吗?

高度似乎输出为ImageLength,这也令人困惑吗?

好吧,你已经有了一个包含键的关联数组,所以使用这些键——它们存在或不存在,但键总是相同的——因此你总是可以使用相同的字面值:

$height =   $exifArray['exif:ImageLength']?? 0;
$width =    $exifArray['exif:ImageWidth']?? 0;

"ImageLength"是EXIF标记的正确名称(官方标准)。历史上,一张图片是(就像今天的位图一样)一行像素,在任意位置切割以移动到下一条(显示)线。有时甚至不需要宽度,因为它可以被假定为每个格式。这就是为什么&;height&;是一个相当现代的术语,而EXIF早在1995年就发布了。

最新更新