Page 1 of 1

How to show static image when there is no dynamic image

Posted: Sun Feb 23, 2014 8:35 am
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 } ?>

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

Posted: Sun Feb 23, 2014 9:24 am
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; ?>

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

Posted: Sun Feb 23, 2014 9:34 am
by parlanchina
Thank you for your suggestion. When I put this code now all dynamic images show but not the static. Any other idea?

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

Posted: Sun Feb 23, 2014 9:40 am
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.

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

Posted: Sun Feb 23, 2014 9:47 am
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?

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

Posted: Sun Feb 23, 2014 10:00 am
by Celauran
You could get the extension with pathinfo and check that it matches against a predefined array.

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

Posted: Sun Feb 23, 2014 11:53 am
by parlanchina
Is it possible to set the code to when img size is = 0Kb show the noimage?