iptcembed() 损坏 JPG 数据

iptcembed() corrupts JPG data

似乎 PHP 的 iptcembed() 函数损坏了 JPG 文件。
此演示提取 IPTC 数据并立即再次嵌入:

// extract IPTC data
getimagesize($sourceFile,$info);
$iptcData = $info['APP13'];

// embed IPTC data
$newFile = iptcembed($iptcData,$sourceFile);

// write new file to disk
$fp = fopen($sourceFile,"w");
fwrite($fp,newFile);
fclose($fp);

// get size of file
$size = getimagesize($sourceFile,$info);

Warning: getimagesize(): corrupt JPEG data: 1382 extraneous bytes before marker in example.php on line XX

怎么了?

PHP bug #77546 2019 年 1 月 30 日报道:

on php 7.3 branch, sometime the stream is not valid

这会影响 PHP 版本 7.3.0、7.3.1 和 7.3.2。
PHP 7.3.3.

中的 patch was issued and the bug is fixed

[2019-02-08 09:40 UTC] nikic@php.net
This change will be part of PHP 7.3.3.

我已确认 PHP 7.2 或 7.4 中不存在此错误。


还提供了一个临时解决方案:

[2019-02-08 05:06 UTC] imagevuex at gmail dot com
Temporary solution, check if the image stream is valid with getimagesizefromstring() before writing to file:

$content = iptcembed($iptc, $file,0);
if($content && @getimagesizefromstring($content)) // is valid

另见 PHP 7.3 Critical Bug Warning,它提供了这种检测图像损坏的方法:

// PHP 7.3 bug https://bugs.php.net/bug.php?id=77546
// detect if image is corrupt before writing
if(
    version_compare(PHP_VERSION, '7.3') >= 0 && 
    version_compare(PHP_VERSION, '7.3.3') < 0 && 
    !@getimagesizefromstring($content)
) return;