vendor/contao-community-alliance/events-contao-bindings/src/Subscribers/ImageSubscriber.php line 111

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of contao-community-alliance/events-contao-bindings
  4.  *
  5.  * (c) 2014-2018 The Contao Community Alliance
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    ContaoCommunityAlliance\Contao\Bindings
  13.  * @subpackage Subscribers
  14.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  15.  * @author     Sven Baumann <baumann.sv@gmail.com>
  16.  * @author     David Molineus <david.molineus@netzmacht.de>
  17.  * @copyright  2018 The Contao Community Alliance.
  18.  * @license    https://github.com/contao-community-alliance/events-contao-bindings/blob/master/LICENSE LGPL-3.0
  19.  * @filesource
  20.  */
  21. namespace ContaoCommunityAlliance\Contao\Bindings\Subscribers;
  22. use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
  23. use Contao\CoreBundle\Image\ImageFactoryInterface;
  24. use Contao\Image;
  25. use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
  26. use ContaoCommunityAlliance\Contao\Bindings\Events\Image\GenerateHtmlEvent;
  27. use ContaoCommunityAlliance\Contao\Bindings\Events\Image\ResizeImageEvent;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. /**
  30.  * Subscriber for the System class in Contao.
  31.  */
  32. class ImageSubscriber implements EventSubscriberInterface
  33. {
  34.     /**
  35.      * The contao framework.
  36.      *
  37.      * @var ContaoFrameworkInterface
  38.      */
  39.     protected $framework;
  40.     /**
  41.      * The image factory.
  42.      *
  43.      * @var ImageFactoryInterface
  44.      */
  45.     private $imageFactory;
  46.     /**
  47.      * Project root dir.
  48.      *
  49.      * @var string
  50.      */
  51.     private $rootDir;
  52.     /**
  53.      * ImageSubscriber constructor.
  54.      *
  55.      * @param ContaoFrameworkInterface $framework    The contao framework.
  56.      * @param ImageFactoryInterface    $imageFactory The image factory.
  57.      * @param string                   $rootDir      Project root dir.
  58.      */
  59.     public function __construct(ContaoFrameworkInterface $frameworkImageFactoryInterface $imageFactory$rootDir)
  60.     {
  61.         $this->framework    $framework;
  62.         $this->imageFactory $imageFactory;
  63.         $this->rootDir      $rootDir;
  64.     }
  65.     /**
  66.      * Returns an array of event names this subscriber wants to listen to.
  67.      *
  68.      * @return array
  69.      */
  70.     public static function getSubscribedEvents()
  71.     {
  72.         return [
  73.             ContaoEvents::IMAGE_RESIZE   => 'handleResize',
  74.             ContaoEvents::IMAGE_GET_HTML => 'handleGenerateHtml',
  75.         ];
  76.     }
  77.     /**
  78.      * Handle a resize image event.
  79.      *
  80.      * @param ResizeImageEvent $event The event.
  81.      *
  82.      * @return void
  83.      */
  84.     public function handleResize(ResizeImageEvent $event)
  85.     {
  86.         $image $this->imageFactory->create(
  87.             $this->rootDir '/' $event->getImage(),
  88.             [$event->getWidth(), $event->getHeight(), $event->getMode()],
  89.             $event->getTarget()
  90.         );
  91.         $event->setResultImage($image->getUrl($this->rootDir));
  92.     }
  93.     /**
  94.      * Handle a get html for image event.
  95.      *
  96.      * @param GenerateHtmlEvent $event The event.
  97.      *
  98.      * @return void
  99.      */
  100.     public function handleGenerateHtml(GenerateHtmlEvent $event)
  101.     {
  102.         /** @var Image $imageAdapter */
  103.         $imageAdapter $this->framework->getAdapter(Image::class);
  104.         $event->setHtml(
  105.             $imageAdapter->getHtml(
  106.                 $event->getSrc(),
  107.                 $event->getAlt(),
  108.                 $event->getAttributes()
  109.             )
  110.         );
  111.     }
  112. }