How to show static image when there is no dynamic image

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
parlanchina
Forum Newbie
Posts: 4
Joined: Sun Feb 23, 2014 8:32 am

How to show static image when there is no dynamic image

Post by parlanchina »

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

Code: Select all

<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?

I am using this code below but instead of dynamic and static images, only the static images show. what is wrong?

Code: Select all

<?php  if(is_file($data['thumbnailUrl'])){ ?>
   <img src="<?php echo $data['thumbnailUrl'];?>" alt="" border="0" width="122" height="92">
<?php }else{ ?>
    <img src="noimage.jpg" width="168" height="128" alt="" />
<?php } ?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to show static image when there is no dynamic image

Post by Celauran »

Is this what you mean?

Code: Select all

<?php if (isset($data['thumbnailUrl']) && !empty($data['thumbnailUrl'])): ?>
<img src="<?= $data['thumbnailUrl']; ?>">
<?php else: ?>
<img src="noimage.jpg">
<?php endif; ?>
parlanchina
Forum Newbie
Posts: 4
Joined: Sun Feb 23, 2014 8:32 am

Re: How to show static image when there is no dynamic image

Post by parlanchina »

Thank you for your suggestion. When I put this code now all dynamic images show but not the static. Any other idea?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to show static image when there is no dynamic image

Post by Celauran »

The logic is good. What's going wrong? My guess is you've got entries for $data['thumbnailUrl'] that aren't empty but also aren't good. Does thumbnailUrl contain a URL or a file path? The latter you can check, the former not so much.
parlanchina
Forum Newbie
Posts: 4
Joined: Sun Feb 23, 2014 8:32 am

Re: How to show static image when there is no dynamic image

Post by parlanchina »

That could be true. When I see the code of a listing that does not have image, this is what it appears - http://imagesus.homeaway.com/mda01/1b79 ... 59a177.1.3. It points to an image but it is broken. Is there a way to fix this with php so whenever there is a broken image the noimage.jpg will display? An example of working image is http://imagesus.homeaway.com/mda01/fb6c ... a635ab.1.3
EDIT:
When I rightclick for the working images it show image type JPEG and for the broken images it shows image type Not Available. Is it possible to structure the script such if it is not a jpeg, gif or png, show noimage?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to show static image when there is no dynamic image

Post by Celauran »

You could get the extension with pathinfo and check that it matches against a predefined array.
parlanchina
Forum Newbie
Posts: 4
Joined: Sun Feb 23, 2014 8:32 am

Re: How to show static image when there is no dynamic image

Post by parlanchina »

Is it possible to set the code to when img size is = 0Kb show the noimage?
Post Reply