Forum for discussing general topics related to Couch.
3 posts Page 1 of 1
When I set an editable for an image and show it, it looks like this:
Code: Select all
<?php require_once('cms/cms.php'); ?>
<cms:template title='TEST' order='1' icon='home'>
  <cms:editable name='image' label="Photo" type='image'/>
</cms:template>
<cms:if image>
  <img src="<cms:show image />">
</cms:if>
<?php COUCH::invoke(); ?>

There are unfortunately customers, who delete or move files via KCFinder and the above code shows an timthumb error plus the image tag is rendered with an non existent image. First I thought, I could use
Code: Select all
<cms:exists image />
but this fails, because the var gives back a full url (https://www.couchtest.com/uploads/image/image.jpg).

Is there a way without PHP to check for the existence of an image (or file).
Tag <cms:exists> only checks for the existence of a file relative to the 'snippets' folder - so, your example was incorrect.

SimonWpt wrote: Is there a way without PHP to check for the existence of an image (or file).

What happens with object tag? Please try and test.

Non-existent images (404) usually do not display the object (width x height = 0 x 0) as in -
Code: Select all
EMPTY:<br/>
<object border="5" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp"></object>
<hr>
OK:<br/>
<object border="5" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg"></object>
<hr>


The <object> element is universal, well supported and provides many interesting capabilities, like events - onerror, onload. You can apply it to any user-uploadables - not exclusively images, but also media, pdfs, docs, files. Note that this element is a paired one, thus you can put as much HTML inside the pair as needed - an alt message to user, links, scripts, tags, a wall of invisible keywords that will appear in HTML for indexing or another placeholder image.

Code: Select all
Replacement image:<br/>
<object data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp">
   <p>Original media not found!<br/>
      <img src="https://interactive-examples.mdn.mozilla.net/media/cc0-images/surfer-240-200.jpg" />
   </p>   
</object>


Events may help you notify Admin or User about some non-existent resource automatically via a JS XHR request or simply log to a file the path / name. Also events help override MSEdge's initial width-height props. For the latter it could be done as follows -
Code: Select all
EMPTY:<br/>
<object border="5" width="0" height="0" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jp" onerror ="myErrorHandler()" onload="mySize();"></object>
<hr>
OK:<br/>
<object border="5" width="0" height="0" data="https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg" onerror ="myErrorHandler()" onload="mySize();"></object>
<hr>

<script>
   function myErrorHandler() {       
      //console.log(event.target);
      event.target.setAttribute("height", "0");
      event.target.setAttribute("width", "0");         
      //alert('Image does not exist:\n\n' + event.target.data);
   }
   function mySize() {       
      //console.log(event.target);
      event.target.setAttribute("height", "auto");
      event.target.setAttribute("width", "auto");         
   }      
</script>


Tag <img> also supports events, although not as comfortable as object in my opinion. See https://stackoverflow.com/questions/927 ... ken-images

Although your question explicitly mentioned "without PHP", I must note that Couch itself is PHP-based, thus any possible new Couch-tag or function intended to simplify work will be based on PHP. If you meant something different, then post appropriately. In my experience I created a couple of functions which return 1 or 0, in case file is found plus readable vs not found or not readable - one is <cms:call 'file_exists' file=myfile />, and another also similarly returns 1 or 0, but works with images only (thus PHP code is much smaller) - <cms:func 'is_image' src=''>. I think for your use-case HTML solution should work just fine. Please report your findings testing the example above.
trendoman wrote: Although your question explicitly mentioned "without PHP", I must note that Couch itself is PHP-based, thus any possible new Couch-tag or function intended to simplify work will be based on PHP. If you meant something different, then post appropriately.


Hello Anton,

thank you for the long answer. I did not express my question clearly. You are right, building an function in PHP is easy:
Code: Select all
function url_exists($url){
return strpos(@get_headers($url)[0],'200') === false ? false : true;
}

But since I use CouchCMS, I often overlooked existing couch solutions. So I was curious, if there is a solution for my common customer problem.

Best regards
3 posts Page 1 of 1