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

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Locked
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

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

Post 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}
User avatar
DIGI3
Dev Team Member
Dev Team Member
Posts: 1609
Joined: Wed Feb 25, 2009 4:25 am
Location: Victoria, BC

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

Post 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.
Not getting the answer you need? CMSMS support options
Sendlingur
Forum Members
Forum Members
Posts: 62
Joined: Wed Aug 08, 2018 4:59 pm

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

Post by Sendlingur »

Thank you so much, this works perfectly :)
Locked

Return to “Modules/Add-Ons”