我使用 php 7.0.22 (Ubuntu 17.04)
当一些用户上传带有截断数据的 PNG 时,我使用:
$a = imagecreatefrompng($path);
然后我崩溃了:
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in ###.php on line ###
我尝试使用
try{...code...}catch(\Throwable $e){...echo...}.
and
$a = @imagecreatefrompng($path);
但脚本只是因为相同的错误消息而终止。
用于imagecreatefromstring(file_get_contents($path))
有相同的结果。
我对 PNG 的验证:我使用函数getimagesize($filename)
回报Array ( [0] => 1045 [1] => 734 [2] => 3 [3] => width="1045" height="734" [bits] => 8 [mime] => image/png )
我该如何处理函数imagecreatefrompng()
or imagecreatefromstring()
带有截断的数据 PNG 文件?
UPD:这是 Ubuntu 上的 PHP7 错误https://bugs.php.net/bug.php?id=73986 https://bugs.php.net/bug.php?id=73986糟糕的作品制作方法:
$output = `php -r "imagecreatefrompng('$fname');" 2>&1`;
if (!empty($output)){
return false; // handle error
} else {
$result = imagecreatefrompng($fname);
}