Reputation: 8095
i am using imagecreatefromstring and currently validate for proper image file format.
So a link of:
swqkdwfibqwfwf
Wont work, because its not a valid file type. But i have just discovered this:
sibdlsibiwbifw.png
Will send without an error from my validation. I get this error for an image link that doesnt return and image:
Warning: file_get_contents(etwteet.png): failed to open stream: No such file or directory in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 141
Warning: imagecreatefromstring(): Empty string or invalid image in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 141
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 143
Is there a way i can catch this error so i can stop the code processing and also notify the user?
Code used to get the URL:
$imagefile = image url;
$resource = imagecreatefromstring(file_get_contents($imagefile));
Thanks. Craig.
Upvotes: 2
Views: 6517
Reputation: 5764
With all implementable validations, I believe it is finally required to capture error on imagecreatefromstring
.
The following syntax is supported on PHP 5.3 or later.
set_error_handler(function ($no, $msg, $file, $line) {
throw new ErrorException($msg, 0, $no, $file, $line);
});
try {
$img = imagecreatefromstring(file_get_contents("..."));
} catch (Exception $e) {
echo $e->getMessage();
}
@
and error_get_last
...if (!$img = @imagecreatefromstring(file_get_contents("..."))) {
$e = error_get_last();
die($e['message']);
}
The following syntax is supported on PHP 5.4 or later.
if (!$img = @imagecreatefromstring(file_get_contents("..."))) {
die(error_get_last()['message']);
}
Upvotes: 6