Jump to content

If image doesn't exist


e1seix

Recommended Posts

Quick query really, is there some sort of if statement we could use to say for example if an image url is invalid to display something else in it's place.

 

eg.

 

image url = "/happy_face.jpg" but there is not such image as "happy_face.jpg", therefore display "default.jpg" or something like that?

Link to comment
https://forums.phpfreaks.com/topic/109999-if-image-doesnt-exist/
Share on other sites

I see what you're getting at, but for me there seems to be a problem - ie. Fatal error: Call to undefined function file_exist().

 

is there something i'm unaware of, or have i just implemented it incorrectly?

 

$picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path

if(!file_exist($picture)) {
   $picture = 'images/thumbs/tn_default.jpg';
}

                        echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>';

 

what i'm trying to say is that if $picture DOESN'T exist, then display default...

 

what's going on?

 

lol many thanks,

the if file exist defo work i am using it on a site i have up and running at the minute.  here is the code i used

 

$image = "";
$broad_img1= "";
$path= 'http://www.yourdomain.com/'; // sets the first half of the path
$web_image_folder = 'image/thumbs/broad/broad1';   // defines the folders to look in
$exts = array('jpg', 'png', 'gif', 'jpeg');  // sets up the array
$image_name = 'thumb_broad1';  
$size = 'width="200" height="200"';

// check for each extension
foreach($exts as $ext) {
   if (file_exists($web_image_folder.'/'.$image_name.'.'.$ext)) {
     $image = $image_name.'.'.$ext;
   } 
   
}

 

change it and use it.

I see what you're getting at, but for me there seems to be a problem - ie. Fatal error: Call to undefined function file_exist().

 

is there something i'm unaware of, or have i just implemented it incorrectly?

 

$picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path

if(!file_exist($picture)) {
   $picture = 'images/thumbs/tn_default.jpg';
}

                        echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>';

 

what i'm trying to say is that if $picture DOESN'T exist, then display default...

 

what's going on?

 

lol many thanks,

 

 

you put file_exist not exists

 

should be

$picture = 'images/thumbs/tn_'.$row[image].'.jpg'; // you may also include the path

if(!file_exists($picture)) {
   $picture = 'images/thumbs/tn_default.jpg';
}

                        echo '<div id="bottomImage" style="background:#ffffff; background-image:url('.$picture.'); background-repeat:no-repeat;"><a href="/viewItem.php?sku='.$row[sku].'"><img height="100px" src="/images/thumbs/tn_mini.gif" width="100px" /></a></div>';

try this:

 

<?php

// series of codes
...

$picture = 'happy_face.jpg'; // you may also include the path

if(!file_exist($picture)) {
   $picture = 'default.jpg';
}

// the rest of the code to load a picture
...
?>

 

cheers,

 

Jay

 

my bad... i forgot to add 's' on file_exist()... it should be file_exists().

 

thanks for the look guys... it should work by now.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.