Tech4Him – Technology with Integrity

A Christian technology chaos wrangler and his thoughts

Developer Blog » Check if external image exists

Posted by Tom On July - 16 - 2009

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: 4% [?]

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • LinkedIn
  • PDF
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Leave a Reply