Developer Blog » Check if external image exists


Maria Birnbaum
Photo by bas:il
So, we have a website that displays images from an external source based upon a URL. For various reasons, sometimes the external source does not have a valid image for the request. In order not to show a broken image to the use, we need to validate that the image is valid.

Thanks to Google (NOT Bing. haha) we found a great little approach that was quickly placed into a Drupal template file.


if (@GetImageSize("http://www.testdomain.com/testimage.gif")) {
  echo "image exists";
} else {
  echo "image does not exist";
}

And according to the site, if you do not have GD installed you can try this code:

if (@fclose(@fopen("http://www.testdomain.com/testimage.gif", "r"))) {
  echo "image exists";
} else {
  echo "image does not exist";
}

We took the concept of the GetImageSize function to handily render web thumb shots from an external service, only if a valid image exists. This way we don’t show broken images to those Internet Explorer users.

via Developer Blog » Check if external image exists.

Popularity: 2% [?]

Tags: ,

About Tom

Christ follower, husband, father, technology and photography enthusiast. Attempting to live life out as a light in this world and stumbling at times in this fallen world. Got a topic you want to have me look into? Did I miss something in a post? Let me know. Just add a comment below.