PHP Fatal error: Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

You are using the fanstactic Gregwar/ImageBundle code to manipulate your images on Symfony2 and twig.
But instead of rendering your website, you get this error on your apache error log file

PHP Fatal error:  Method Gregwar\\ImageBundle\\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

or directly on your html genered code

<img src="
Fatal error: Method Gregwar\ImageBundle\ImageHandler::__toString() must not throw an exception in /home/www/recette/app/cache/dev/classes.php on line 7426

this issue may be a rights problem on your cache drectory. By default, gregwar cache directory is on your web so, launch this command

chmod 777 web/cache

This error may also occur if the file doesn’t exist, for a clean code, add

{% if file_exists('uploads/' ~ site.id ~ '.jpg') %}
    {{ image(site.picture).resize(250, 50) }}
{% endif %}

or on twig code

{% if site.has_picture %}
    {{ image(site.picture).resize(250 ,50) }}
{% endif %}

For more informations about file_exists, see here Symfony2 twig add new function file_exists

Leave a Reply