homepage Welcome to WebmasterWorld Guest from 54.242.231.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to show static image when there is no dynamic image
virtualreality




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

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




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

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




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

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




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

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




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

<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="">
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved