How to serve an "Image Not Available" file...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zander213
Forum Newbie
Posts: 13
Joined: Mon Aug 23, 2004 1:46 pm

How to serve an "Image Not Available" file...

Post by zander213 »

I am displaying real estate listings on a page - and trying to work through a couple of issues i'm having (pretty new at php)...

I'm basically trying to do 3 things here:

1. When in image is not available, the "image not available" file should be served to the page.
2. When a thumbnail is not available it should not be served.
3. When someone clicks on the thumbnail, the main image should change to the selected image.

If anyone is interested in having a look at my code and getting all or some of these 3 things working - I would be more than happy to send you a tip for your assistance!

Here are some details you need:

Here is the link to the detail page:
http://sanluisrealtor.studiocreek.com/d ... ber=106107

Here is the complete php: (yes, I know it's not too pretty ;) )
http://studiocreek.com/sanluisrealtor/detail_php.txt

Here is the "image not available" graphic I need to swap if the image doesn't exist:
http://sanluisrealtor.studiocreek.com/m ... ilable.png

Again, if anyone can give me a hand I will be more than happy to provide a tip. This should be a piece of cake - right?

Thanks!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

If the MLS table is like the MLS table in FL then there is a field for the number of photos included. Can't you just go off of that?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You could conjur up a PHP image script -

Code: Select all

<?php

$images = array( /* allowed images go here.. could also be from a table, or a file, is only an array for the purposes of this example */ );
$image = ((isset($_GET['iid'])) && (array_key_exists($_GET['iid'], $images)) ? $images[$_GET['image']] : 'imagenotfound.jpg'); // iid = image id

readfile('/path/to/images/' . $image);

?>
Then link to it using the GET variable iid to identify the image you want to use.

Or - use Apaches 404 error handling to serve up the wanted image, though this could be difficult with some hosting companies policies of use etc. :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or you could serve your images using simple function like:

Code: Select all

function image($image_name) {
   static $path = "images/";
   if(file_exists($path . basename($image_name))) {
      return '<img src="' . $path . basename($image_name) . '" />';
   } else {
      return '<img src="' . $path . 'na.png' . '" />';
   }
}


echo image('realImage1.png');
echo image('nonexistentImage.png');
echo image('realImage2.png');
MaNiAC
Forum Newbie
Posts: 20
Joined: Fri Dec 23, 2005 4:20 am

Post by MaNiAC »

Code: Select all

<div class="main_image"> <img src="http://sanluisrealtor.studiocreek.com/mls/images/<?php echo $row_recordset['ML_Number']; ?>.jpg" align="left">
Replace with:

Code: Select all

<?
if( $row_recordset['ML_Number'] != ""){
  $img = $row_recordset['ML_Number'];
} else {
  $img = "not_available.png";
}
?>
<div class="main_image"> <img src="http://sanluisrealtor.studiocreek.com/mls/images/<?php echo $img; ?>.jpg" align="left">
or

Code: Select all

<?
if( $row_recordset['ML_Number'] != ""){
?><div class="main_image"> <img src="http://sanluisrealtor.studiocreek.com/mls/images/<?php echo $row_recordset['ML_Number']; ?>.jpg" align="left"><?
} else {
?><div class="main_image"> <img src="http://sanluisrealtor.studiocreek.com/mls/images/not_available.png" align="left"><?
}
?>

Once again, I haven't tested it out. I'm sure something like this would work.
Post Reply