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...}.


$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
Bad way to make works:

$output = `php -r "imagecreatefrompng('$fname');" 2>&1`;
if (!empty($output)){
    return false; // handle error
} else {
    $result = imagecreatefrompng($fname);