我用php 7.0.22(Ubuntu 17.04)
当某些用户使用截断数据上传PNG时,我使用:
$a = imagecreatefrompng($path);
Run Code Online (Sandbox Code Playgroud)
然后我崩溃了:
Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in ###.php on line ###
Run Code Online (Sandbox Code Playgroud)
我试着用
try{...code...}catch(\Throwable $e){...echo...}.
Run Code Online (Sandbox Code Playgroud)
和
$a = @imagecreatefrompng($path);
Run Code Online (Sandbox Code Playgroud)
但脚本只是死了同样的错误信息.使用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()或imagecreatefromstring()截断数据PNG文件?
UPD:这是Ubuntu上的一个PHP7错误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);
}
Run Code Online (Sandbox Code Playgroud)