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

问题内容:

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);
}

问题评论:

原文地址:

https://stackoverflow.com/questions/45936271/how-to-handle-gd-png-fatal-libpng-error-read-error-truncated-data-in-php-7

Tags:,

Add a Comment