Jump to content


replacing broken images with a default image


11 replies to this topic

#1 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

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.

#2 djones

    Young Padawan

  • Members
  • Pip
  • 61 posts
  • Location:Winston-Salem, NC

Posted 02 December 2005 - 08:34 AM

Are you pulling images from a database or just straight off the server?

#3 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

Posted 02 December 2005 - 03:32 PM

View Postdjones, on Dec 2 2005, 08:34 AM, said:

Are you pulling images from a database or just straight off the server?

I have it in an images/ folder.My host doesn't allow databases

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

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 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

Posted 02 December 2005 - 07:10 PM

They allow me PHP and SSI

#6 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

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

#7 coolaid

    P2L Jedi Master

  • Members
  • PipPipPipPip
  • 1,435 posts
  • Gender:Male
  • Interests:i wonder..

Posted 07 December 2005 - 07:06 PM

well maybe try a google of: replacing broken images php ...

#8 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

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 Av-

    I Feel Left Out

  • Members
  • PipPipPipPip
  • 1,971 posts
  • Gender:Male
  • Location:10 ft. below sea level

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 shawna

    Young Padawan

  • Members
  • Pip
  • 46 posts
  • Gender:Female
  • Location:Jacksonville,Florida
  • Interests:Web Design,music,I'm a vocalist and wannabe guitarist. I'm a female of course if you haven't noticed by my name. I'm a vegetarian,and I like to help people out when I can.99% of the time,I'm partially wrong : ) so don't yell at me if I am kaythxbye

Posted 18 December 2005 - 05:45 AM

AWESOME! Thank you so much!

#11 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

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.

#12 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

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