drath Posted July 19, 2008 Share Posted July 19, 2008 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? Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 19, 2008 Share Posted July 19, 2008 <?php $link = 'path/to/image.ext'; if(!file($link)) { //Image is not avail } else { echo '<img src="' . $link . '" />'; } Quote Link to comment Share on other sites More sharing options...
drath Posted July 20, 2008 Author Share Posted July 20, 2008 This would only work if I am looking for images stored on my site, but what about external images? I think I'm going to have to use some Javascript. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 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 Quote Link to comment Share on other sites More sharing options...
drath Posted July 20, 2008 Author Share Posted July 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 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? Quote Link to comment Share on other sites More sharing options...
drath Posted July 20, 2008 Author Share Posted July 20, 2008 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. Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 20, 2008 Share Posted July 20, 2008 With the code I gave above you can use set the filename to http://doamin.tld/path/to/file.ext and it should work just fine... I'll test it in a few when the crons on my server stop running and confirm. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 With the code I gave above you can use set the filename to http://doamin.tld/path/to/file.ext and it should work just fine Yes it will work fine, but no it will not guarantee the file is an image which is the point. Imagegetsize should lag at all Quote Link to comment Share on other sites More sharing options...
drath Posted July 20, 2008 Author Share Posted July 20, 2008 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? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 20, 2008 Share Posted July 20, 2008 look at chmods on php.net and you can detect a files perm settings Quote Link to comment Share on other sites More sharing options...
drath Posted July 21, 2008 Author Share Posted July 21, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.