I'm trying to detect if an image is an image when given a URL. To save an image from a url I used the following:
// create the image and save it
$imagefile = $URL;
$resource = imagecreatefromstring(file_get_contents($imagefile));
// X amount of quality is the last number
imagejpeg($resource, "images/covers/$imagepath.jpeg", 50);
imagedestroy($resource);
The $URL is simply an image link provided by the user.
I have tried the following:
$imagefile = $addBuildCover;
$resource = imagecreatefromstring(file_get_contents($imagefile));
if ($resource !== false) {
$return['imageGood'] = true;
} else {
$return['imageBad'] = true;
}
I have tried that code and i returns the correct JSON return of 'imageBad' but it's giving me an error before which is:
Warning: file_get_contents(ewffw.png): failed to open stream: No such file or directory in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 116
Warning: imagecreatefromstring(): Empty string or invalid image in /var/www/clients/client2/web3/web/process/addnewbuild.php on line 116
How do I try I catch the URL fail but not actually have an error return like above?