How to replace missing images with default image

Discussion in 'Programming' started by smitts, Dec 31, 2006.

  1. #1
    Is there a way to detect missing images (not residing on my site) and redirect them to a static image?

    Basically if I had an image that I was displaying from a different site (with permission), and that image were removed, is there a way I can default that image to a default .gif? I have tried PHP getimagesize, but to do that multiple times per page REALLY slowed things down.

    Is there a better way? The only other thing I could think of was to create a background image of the default gif, load the images on top of the background, and use blank alt text so that if the image I'm trying to load doesn't exist, just the background default image will show, but this won't work very well, as sometimes the background image would show through as the size of the images are variable.

    Thanks!
     
    smitts, Dec 31, 2006 IP
  2. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #2
    I'm sure that you could actually do this with javascript preloading
     
    frankcow, Dec 31, 2006 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Try htaccess

    
    RewriteCond %{ENV:ERROR404} \.(jpe?g|gif|png)$ [NC]
    RewriteRule ^.*$ foo.gif [L]
    
    Code (markup):
     
    nico_swd, Dec 31, 2006 IP
    Karen May Jones likes this.
  4. frankcow

    frankcow Well-Known Member

    Messages:
    4,859
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    180
    #4
    well done, that's an excellent and efficient solution!
     
    frankcow, Dec 31, 2006 IP
  5. smitts

    smitts Peon

    Messages:
    177
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, thank you very much!
     
    smitts, Jan 2, 2007 IP