PHP Code to Check if Image Exists or not : Ensure Your Website Displays Images Correctly
Check if given url is image or not , also check if link exist or not
<?php
function is_image_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_status == 200) {
$image_info = getimagesize($url);
if ($image_info !== false) {
return true;
}else{
return false;
}
} else {
return false;
}
}
$image_url = 'https://cdn-static.roomster.com/pics/Original/L-27092602-c78c872fa5074d9ea8f62a33108679e5.jpg';
if(is_image_exist($image_url)){ ?>
<img class="img img-fluid" src="<?php echo $image_url;?>">
<?php }else{ ?>
<img class="img img-fluid" src="https://dummyimage.com/200x200/000/fff&text=404">
<?php } ?>