bundles/Jabber/RestApiBundle/EventListener/ThumbnailListener.php line 12

Open in your IDE?
  1. <?php 
  2. namespace Jabber\RestApiBundle\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. class ThumbnailListener {
  7.     
  8.     public function onRequest(RequestEvent $e) {
  9.         $pathInfo $e->getRequest()->getPathInfo();
  10.         if (str_contains($pathInfo'image-thumb') && str_contains($pathInfo'dynamic')) {
  11.             if (!file_exists(PIMCORE_WEB_ROOT '/var/tmp/thumbnails' $pathInfo)) {
  12.                 $pathParts explode("/"$pathInfo);
  13.                 $thumb $pathParts[count($pathParts)-2];
  14.                 $thumbParts explode("__"$thumb);
  15.                 $configName $thumbParts[count($thumbParts)-1];
  16.                 $assetId $pathParts[count($pathParts)-3];
  17.                 $thumbPath $this->generateThumbnail($assetId$configName);
  18.                 if (!empty($thumbPath)) {
  19.                     $response = new Response();
  20.                     $response->headers->set('Content-Type''image/png');
  21.                     $response->setContent(file_get_contents($thumbPath));
  22.                     $e->setResponse($response);
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     public function generateThumbnail($assetId$configName) {
  28.         $config null;
  29.         $image \Pimcore\Model\Asset\Image::getById($assetId);
  30.         $thumbnail = new \Pimcore\Model\Asset\Image\Thumbnail\Config();
  31.         $properties $image->getProperties();
  32.         $configPrefix explode("-"$configName)[0];
  33.         if (isset($properties[$configPrefix '_height'])) {
  34.             $config = [
  35.                 'width' => $properties[$configPrefix '_width']->getData(),
  36.                 'height' => $properties[$configPrefix '_height']->getData(),
  37.                 'x' => $properties[$configPrefix '_x']->getData(),
  38.                 'y' => $properties[$configPrefix '_y']->getData(),
  39.             ];
  40.         }
  41.         if (!empty($config)) {
  42.             $thumbnail->addItem('crop'$config);
  43.             $thumbnail->setName($configName);
  44.             $newImage $image->getThumbnail($thumbnailfalse);
  45.             
  46.             return PIMCORE_WEB_ROOT '/var/tmp/thumbnails' $newImage->getPath();
  47.         }
  48.     }
  49. }