replacing broken images with a default image
Started by shawna, Dec 02 2005 01:33 AM
11 replies to this topic
#1
Posted 02 December 2005 - 01:33 AM
Hello again,
I need to know if there's a code out there that automaticly takes your set default image and replace any broken images. Instead of people seeing the "red x's" I wanted to have any broken image replaced by one that says "photo not available". Almost like 404 error. I just have no idea what this kind of thing is called.
I need to know if there's a code out there that automaticly takes your set default image and replace any broken images. Instead of people seeing the "red x's" I wanted to have any broken image replaced by one that says "photo not available". Almost like 404 error. I just have no idea what this kind of thing is called.
#2
Posted 02 December 2005 - 08:34 AM
Are you pulling images from a database or just straight off the server?
#4
Posted 02 December 2005 - 05:27 PM
The only way i know of to do it is .htaccess, but if your host doesn't allow databases, they might not allow that either. And to do it is a little beyond what i know about it right now, but you can look at the link in "the answers to all your questions" in the PHP forum.
#5
Posted 02 December 2005 - 07:10 PM
They allow me PHP and SSI
#6
Posted 06 December 2005 - 09:42 PM
Can someone please let me know what it's called so that maybe I could find a tut by googling it?
-thanks for the tips
-thanks for the tips
#7
Posted 07 December 2005 - 07:06 PM
well maybe try a google of: replacing broken images php ...
#8
Posted 07 December 2005 - 09:55 PM
nope,I've found nothing. Everything that came up talks about the browser settings but not how to make your website do that. But thanks for the help. I even search image placeholders and .htaccess with the same two and I got nothing. I guess I will have to just catch broken images when I see them.
#9
Posted 18 December 2005 - 05:12 AM
you could do this with PHP
<?php
$imagesource = "path/to/image.gif";
if (file_exists($imagesource)) {
echo "<img src=\"$imagesource\" />";
} else {
echo '<img src="path/to/alternative_image.gif" />';
}
?>
Edited by Avalanche, 18 December 2005 - 05:13 AM.
#10
Posted 18 December 2005 - 05:45 AM
AWESOME! Thank you so much!
#11
Posted 20 December 2005 - 05:06 AM
O_O;;
I understand that is what you are asking for, but you are really going to put that code for every single image on the page?
Thats pretty crazy man.
Avalanche, I think what me means is a global script that would automatically do that. What I was thinking of doing is to run a function to find all the image tags and check them if they are valid and if they arn't change them to the default image, but that would be kinda complicated if you ask me.
I understand that is what you are asking for, but you are really going to put that code for every single image on the page?
Thats pretty crazy man.
Avalanche, I think what me means is a global script that would automatically do that. What I was thinking of doing is to run a function to find all the image tags and check them if they are valid and if they arn't change them to the default image, but that would be kinda complicated if you ask me.
#12
Posted 20 December 2005 - 03:03 PM
<script type="text/javascript">
function imageCheck(){
var imgs = getElementsByTagName('img');
for(x=0; x < imgs.length; x++){
imgs[x].onerror = function (event){ img.src = 'path/to/default_img.gif'; }
}
}
window.onload = imageCheck;
</script>
I'm not sure as to whether or not onerror is the right event to look for, but it's basically about that simple (i believe, i have not tested it).Just place it at the bottom of your page and see what happens.
Edited by rc69, 20 December 2005 - 03:04 PM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
