Sometimes we need to check the image link valid or invalid. So we have to solve this problem. In PHP we can check a link by curl. Executing curl get URL we can check a URL is valid or not. Also, we can use @getimagesize for image link validation.
Solution 1: Using CURL
I have created the following function to check the image URL is valid or invalid. If we get a curl to execute the result then it is a valid image URL otherwise it is a broken URL.
function checkRemoteFile($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if($result !== FALSE)
{
return true;
}
else
{
return false;
}
}
Solution 2: Using @getimagesize
It is very simple. I have created the following function to check the image URL.
function CheckImageLink($external_link){
if (@getimagesize($external_link)) {
return true;
} else {
return false;
}
}