1

based on this post

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:

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

Any help is appreciated. Thank you.

UPD My twig function is as follows:

<?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';
 }
}
Community
  • 1
  • 1
Jack
  • 857
  • 14
  • 39
  • what is the code for your twig function? – Rooneyl Nov 01 '16 at 09:59
  • is `author.image` the absolute path of the image? probably not – Federkun Nov 01 '16 at 10:02
  • author.image is a remote image, something like https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/14488260_1668659883445000_18272225396260864_n.jpg – Jack Nov 01 '16 at 10:03
  • Where is the actual file_exists function code in your update? Assuming you model getter returns false if no image, why not just do `if not author.image` – Rooneyl Nov 01 '16 at 10:04
  • then you don't need `file_exists`. You can get the headers of the resource, and check if the status is 404 or not. If it's 404, then the image doesn't exists. – Federkun Nov 01 '16 at 10:04
  • As far as I understand, file_exists is a standard twig function which I reference to. I dont have its code – Jack Nov 01 '16 at 10:05
  • @Jack I may be wrong but I've never seen a standard twig function for file_exists. Have checked http://twig.sensiolabs.org/documentation and it isn't there – Rooneyl Nov 01 '16 at 10:08
  • @Rooneyl, a php string is a callback as well. – Federkun Nov 01 '16 at 10:09

1 Answers1

3

Well, the Twig-Extension you created uses the PHP-Function file_exists, which is only working for local files.
In order to make it work for remote files you would need to change it like this (not tested):

<?php

namespace AppBundle\Twig\Extension;

class FileExtension extends \Twig_Extension
{
    /**
     * Return the functions registered as twig extensions
     *
     * @return array
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('remote_file_exists', [$this, 'remoteFileExists']),
        ];
    }

    /**
     * @param string $url
     *
     * @return bool
     */
    public function remoteFileExists($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return $status === 200 ? true : false;
    }

    public function getName()
    {
        return 'app_file';
    }
}

?>

Now you should be able to use the Twig-Function remote_file_exists to check whether your image exists.

Justus Klein
  • 123
  • 7