Page 1 of 1

how to not display img tag if image don´t exists

Posted: Thu Mar 14, 2019 10:19 am
by Sendlingur
Im using this loop to loop through a huge database. It takes the content images and puts them in the <img> tag.

In most cases the image is still around and every thing works fine. But in some cases the image doesn't exist and the img url is broken and as a result it displays the no image icon.

the thing is that I'm not sure how to make the loop detect if there is an actual file behind the img url or if the file is missing.

I've tried to use
empty, !empty, ==file, etc.
but obviously the loop is reading the broken urls as imgs.

below is the loop I'm using, can someone please advise me

Code: Select all

     {cgsi_getimages assign='imageinfo' nocontent=1}{$entry->content}{/cgsi_getimages}
      {foreach from=$imageinfo[0].src item=image}
        {if empty($image)}
      <img src="uploads/images/img-01.jpg" class="sidebar-img" alt="{$entry->title}"/>
        {else}   
           <img src="{$image}" class="sidebar-img" alt="{$entry->title}">
        {/if}
      {/foreach}

Re: how to not display img tag if image don´t exists

Posted: Thu Mar 14, 2019 3:45 pm
by DIGI3
I haven't tested, but you should be able to use {if file_exists($image)}
You may need to enable permissive smarty in your config file.

Re: how to not display img tag if image don´t exists

Posted: Fri Mar 15, 2019 9:48 am
by Sendlingur
Thank you so much, this works perfectly :)