ImageTwig.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Gregwar\ImageBundle\Extensions;
  3. use Symfony\Component\Templating\EngineInterface;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\Form\FormView;
  6. /**
  7. * ImageTwig extension
  8. *
  9. * @author Gregwar <g.passault@gmail.com>
  10. * @author bzikarsky <benjamin.zikarsky@perbility.de>
  11. */
  12. class ImageTwig extends \Twig_Extension
  13. {
  14. private $container;
  15. private $environment;
  16. public function __construct(ContainerInterface $container)
  17. {
  18. $this->container = $container;
  19. }
  20. public function initRuntime(\Twig_Environment $environment)
  21. {
  22. $this->environment = $environment;
  23. }
  24. public function getFunctions()
  25. {
  26. return array(
  27. 'image' => new \Twig_Function_Method($this, 'image', array('is_safe' => array('html'))),
  28. 'new_image' => new \Twig_Function_Method($this, 'newImage', array('is_safe' => array('html')))
  29. );
  30. }
  31. public function image($path)
  32. {
  33. return $this->container->get('image.handling')->open($path);
  34. }
  35. public function newImage($width, $height)
  36. {
  37. return $this->container->get('image.handling')->create($width, $height);
  38. }
  39. public function getName()
  40. {
  41. return 'image';
  42. }
  43. }