symfony2 twig check if a remote image exists
<p>based on <a href="https://stackoverflow.com/questions/14231967/symfony2-checking-if-file-exists">this</a> post</p>

<p>I am trying to check if a remote image exists (loads correctly), and if it does, display it, othervise display default image. I have already done a twig extension and corrected the code but it always returns false though I definetely know that the image exists. My twig template code is as follows:</p>

<pre><code>{% if file_exists(author.image) %} //always get false here so the default image is loaded
 &lt;img src="{{ author.image }}" alt="{{ author.name }}"&gt;//loads an image correctly if outside condition 
{% else %}
 &lt;img src="/var/www/web/img/no_image.png" alt="no_image"&gt;
{% endif %}
</code></pre>

<p>Any help is appreciated. Thank you.</p>

<p><strong>UPD</strong> My twig function is as follows:</p>

<pre><code>&lt;?php
namespace AppBundle\Twig\Extension;
class FileExtension extends \Twig_Extension
{

/**
 * Return the functions registered as twig extensions
 * 
 * @return array
 */
 public function getFunctions()
 {
    return array(
        new \Twig_SimpleFunction('file_exists', 'file_exists'),
    );
 }

 public function getName()
 {
    return 'app_file';
 }
}
</code></pre>