Welcome to WebmasterWorld Guest from 54.227.101.214

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to show static image when there is no dynamic image

     

virtualreality

12:43 pm on Feb 21, 2014 (gmt 0)

5+ Year Member



Hello, I have a script that shows dynamic images, this is the script:

<img src="<?php echo $data['thumbnailUrl'];?>" alt="" border="0" width="122" height="92">


When there is no image - $data['thumbnailUrl'], broken image box is displayed. How can I edit this script so when there is no a dynamic image, a static one will show instead?

THANKS!

rainborick

4:31 pm on Feb 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest way is to add some code before the <img> tag that determines which image will be shown. Something like:

<?php
$thumbImgUrl = $staticThumbUrl;
if (file_exists($data['thumbnailUrl'])) {
$thumbImgUrl = $data['thumbnailUrl'];
}
?>
<img src="<?php echo $thumbImgUrl;?>" alt="" border="0" width="122" height="92">

virtualreality

10:53 am on Feb 23, 2014 (gmt 0)

5+ Year Member



Thanks for your reply. This is what I tried but it does not work. Do you know what might be wrong?


<img src="<?php if(is_file($data['thumbnailUrl'])){
<img src="<?php echo $data['thumbnailUrl'];?>" alt="" border="0" width="122" height="92"></a>
}else{
<img src="noimage.jpg" width="168" height="128" alt="" /></a>
}
?>" alt="" border="0" width="122" height="92"></a>

lucy24

9:15 pm on Feb 23, 2014 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



if (file_exists
if(is_file

Did you try both? Assuming for the sake of discussion that you're not dealing with >2Gig images.

This function will return FALSE for symlinks pointing to non-existing files.

Well, you wouldn't be doing that, would you?

it does not work

Hah, the dreaded "does not work". Does your code yield false negatives, false positives or both?

Do you really give the alt/width/height declarations twice (once inside the function, once outside) or was that a typo? rainborick's method seems thriftier, though you'd need to tweak it a bit if your alternative image is different from the "real" one.

The results of this function are cached.

Thank you, php dot net, for that timely reminder. Clear your cache or, better yet, try a whole new browser.

penders

9:45 am on Feb 24, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



<img src="<?php if(is_file($data['thumbnailUrl'])){
<img src="<?php echo $data['thumbnailUrl'];?>" alt="" ....


You appear to be trying to embed an IMG element inside the SRC attribute of another IMG element?!

However, the fundamental problem here is that
$data['thumbnailUrl']
is presumably a client-side URL and
is_file()
(or
file_exists()
) check a server-side filesystem path - so this is unlikely to be successful. Maybe you already have the filesystem path in your $data array? If not, you'll need to construct this from the URL.

Alternatively, you could try a client-side / JavaScript solution:
<img src="<?=$data['thumbnailUrl']?>" onerror="this.src='/images/image-does-not-exist.png'" alt="">
 

Featured Threads

Hot Threads This Week

Hot Threads This Month