How to handle “gd-png: fatal libpng error: Read Error: truncated data” in php 7

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

I use php 7.0.22 (Ubuntu 17.04)

When some user upload PNG with truncated data and i use:

$a = imagecreatefrompng($path); 

then I get crash with:

Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in ###.php on line ### 

I try to use

try{...code...}catch(\Throwable $e){...echo...}. 

and

$a = @imagecreatefrompng($path); 

But script just dies with same error message. Use of imagecreatefromstring(file_get_contents($path)) has same result.

My validation of PNG: I use function getimagesize($filename) returns Array ( [0] => 1045 [1] => 734 [2] => 3 [3] => width="1045" height="734" [bits] => 8 [mime] => image/png )

How can I handle function imagecreatefrompng() or imagecreatefromstring() with truncated data PNG file?

UPD: It's a PHP7 bug on Ubuntu https://bugs.php.net/bug.php?id=73986 Bad way to make works:

$output = `php -r "imagecreatefrompng('$fname');" 2>&1`; if (!empty($output)){     return false; // handle error } else {     $result = imagecreatefrompng($fname); } 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!