13

I am in the middle of coding up a property portal. I am stuck on checking images. I know how to check if an image url is set. But the problem is detecting if there is actually a valid image at the url.

example : http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg

This image url exists but the image is has now been removed so it just displays blank in my propety search page. Is there a way of checking there is an image there at the url and then displaying a placeholder if it doesnt exist.

something like

$imageURL = "http://property.images.themovechannel.com/cache/7217/6094437/img_main.jpg"; if (exists($imageURL)) { display image } else { display placeholder } 

But all this does is check the url exists, which it does there is just no image there

Thanks in advance

3
  • Perhaps you can look for an <img> tag in the returned HTML? Commented Feb 20, 2013 at 0:41
  • I was thinking of that but the page is quite big kind of wanted to do it all on the fly with php Commented Feb 20, 2013 at 0:44
  • Could you post a link to an image that does exist? Commented Feb 20, 2013 at 0:46

3 Answers 3

28

Use getimagesize() to ensure that the URL points to a valid image.

if (getimagesize($imageURL) !== false) { // display image } 
Sign up to request clarification or add additional context in comments.

2 Comments

It's very slow function for external URI.
if file doesn't exist it cause php Notice. Use this function with "@" is bad manner, so I think better way is that @plutov.by described
8
function exists($uri) { $ch = curl_init($uri); curl_setopt($ch, CURLOPT_NOBODY, true); curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $code == 200; } 

Comments

1
function is_webUrl($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); if (curl_exec($ch) !== FALSE) { return true; } else { return false; } } if(is_webUrl('http://www.themes.tatwerat.com/wp/ah-personal/wp-content/uploads/2016/08/features-ah-wp-view.jpg')) { echo 'yes i found it'; }else{ echo 'file not found'; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.