Jump to content

PHP Detect Broken Image


drath

Recommended Posts

I know there is several ways to do this in JavaScript like the using the "onerror" property to change the image, but I don't want to just change the image; I want to not have the image tag called at all. Does anybody know how this could be accomplished?

Link to comment
Share on other sites

I've used this function I made up before

<?php
is_img($img_path){
$image_types = array("image/jpeg", "image/jpg", "image/pjpg", "image/pjpeg", "image/gif", "image/pgif", "image/png", "image/ppng");
$data = getimagesize($img_path);
if(in_array($data[3],$image_types)){
	return TRUE;
}
else{
	return FALSE;
}
}
?>

 

If its a valid image type it returns true else its false

Link to comment
Share on other sites

Hmm, I never knew about getimagesize before, it seems to be working. The only issue I am having is it will print an warning/error when it can't find the image (when it's actually broken). Is there any way to handle the error and create a custom echo or something? I've never error handled before in PHP.

Link to comment
Share on other sites

us it in a if command

example

<?php
#assume the function is in this file (the is_img function I wrote)
$img = new Img_Class
if(is_img($img->path)){
#its good do what you want
}
else{
#use your custom class error reporting
$img->error_report("Its a broken image");
}
?>

 

Get it?

Link to comment
Share on other sites

That seems to be working, thanks for all the help. It seems like getimagesize has a really long timeout, so the pages are taking a really long time to load. I wonder if there is a way to lower the timeout or something. It's really lagging in some cases.

Link to comment
Share on other sites

Hmmm, well it just tried both the methods and ukwntech's file method seems to be much faster. Perhaps my server is just slow with PHP, but the imagegetsize really seems to get slow at some points. There does however seem to be a flaw using the file method... it doesn't detect if it's forbidden... although I suspect that would be a problem with the imagegetsize as well. Any ideas for blocking forbidden images?

Link to comment
Share on other sites

Actually, both the methods you guys supplied result in one thing:

 

Slowness rendering the page.

 

The reason is, because I am trying to find images one after another which means if it can't find one, it will stop the whole page until it's sure it's not available (based on timeout?). Both methods also mean it will check on each image which may be fast or slow depending on the host of the image, some take a long time to respond.

 

I now currently use a javascript that will replace the broken image after everything is loaded instead of PHP trying to check one by one. It's speed is incredibly faster; however, I don't have the flexibility of PHP rendering custom messages and stuff when they fail, which is why I still want a PHP method that is faster... I just can't get it to do so :).

 

*EDIT*

 

One idea would be to let PHP load all the images in, then check each image after, and then somehow replace the information (the div and img tags) with nothing, or blank. Although, I wouldn't know how to replace already echo'd information.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.