src/EventListener/ImageUploadListener.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Enums\JvnMediaTypeEnum;
  4. use App\Entity\JvnMedia;
  5. use App\Handler\ThumbnailHandler;
  6. use App\Helpers\Utils;
  7. use Imagine\Image\Box;
  8. use League\Flysystem\FileExistsException;
  9. use League\Flysystem\FilesystemInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Vich\UploaderBundle\Event\Event;
  12. use Vich\UploaderBundle\Storage\StorageInterface;
  13. class ImageUploadListener
  14. {
  15.     private ThumbnailHandler $thumbnailHandler;
  16.     private StorageInterface $vichStorage;
  17.     private FilesystemInterface $mediaStorage;
  18.     private Security $security;
  19.     public function __construct(
  20.         ThumbnailHandler $thumbnailHandler,
  21.         StorageInterface $vichStorage,
  22.         FilesystemInterface $mediaStorage,
  23.         Security $security
  24.     )
  25.     {
  26.         $this
  27.             ->setThumbnailHandler($thumbnailHandler)
  28.             ->setVichStorage($vichStorage)
  29.             ->setMediaStorage($mediaStorage)
  30.             ->setSecurity($security)
  31.         ;
  32.     }
  33.     /**
  34.      * @param Event $event
  35.      * @throws FileExistsException
  36.      */
  37.     public function onPostUpload(Event $event)
  38.     {
  39.         /** @var JvnMedia $image */
  40.         $image $event->getObject();
  41.         if( $image->getType() === JvnMediaTypeEnum::get(JvnMediaTypeEnum::PHOTO) ) {
  42.             $thumbnail = clone $image;
  43.             $thumbnail->setFilename(Utils::thumbnailName($image->getFilename()));
  44.             $image $this
  45.                 ->getThumbnailHandler()
  46.                 ->getImagine()
  47.                 ->read($this->getVichStorage()->resolveStream($image'file'))
  48.                 ->resize(
  49.                     new Box($this->getThumbnailHandler()->getWidth(), $this->getThumbnailHandler()->getHeight())
  50.                 )
  51.                 ->strip()
  52.             ;
  53.             $thumbnailFile $this->getVichStorage()->resolvePath($thumbnail'file');
  54.             // Vich Uploader use prefix and to avoid double prefix we need to reset
  55.             $this->getMediaStorage()->getAdapter()->setPathPrefix(null);
  56.             if( ! $this->getMediaStorage()->has($thumbnailFile) ) {
  57.                 $this->getMediaStorage()->write($thumbnailFile$image->get('jpeg'));
  58.             }
  59.             // We unset thumbnail object to avoid problems with ORM persistence
  60.             unset($thumbnail);
  61.         }
  62.     }
  63.     /**
  64.      * @return ThumbnailHandler
  65.      */
  66.     public function getThumbnailHandler(): ThumbnailHandler {
  67.         return $this->thumbnailHandler;
  68.     }
  69.     /**
  70.      * @param ThumbnailHandler $thumbnailHandler
  71.      * @return ImageUploadListener
  72.      */
  73.     public function setThumbnailHandler(ThumbnailHandler $thumbnailHandler): ImageUploadListener {
  74.         $this->thumbnailHandler $thumbnailHandler;
  75.         return $this;
  76.     }
  77.     /**
  78.      * @return StorageInterface
  79.      */
  80.     public function getVichStorage(): StorageInterface {
  81.         return $this->vichStorage;
  82.     }
  83.     /**
  84.      * @param StorageInterface $vichStorage
  85.      * @return ImageUploadListener
  86.      */
  87.     public function setVichStorage(StorageInterface $vichStorage): ImageUploadListener {
  88.         $this->vichStorage $vichStorage;
  89.         return $this;
  90.     }
  91.     /**
  92.      * @return FilesystemInterface
  93.      */
  94.     public function getMediaStorage(): FilesystemInterface {
  95.         return $this->mediaStorage;
  96.     }
  97.     /**
  98.      * @param FilesystemInterface $mediaStorage
  99.      * @return ImageUploadListener
  100.      */
  101.     public function setMediaStorage(FilesystemInterface $mediaStorage): ImageUploadListener {
  102.         $this->mediaStorage $mediaStorage;
  103.         return $this;
  104.     }
  105.     /**
  106.      * @return Security
  107.      */
  108.     public function getSecurity(): Security {
  109.         return $this->security;
  110.     }
  111.     /**
  112.      * @param Security $security
  113.      * @return ImageUploadListener
  114.      */
  115.     public function setSecurity(Security $security): ImageUploadListener {
  116.         $this->security $security;
  117.         return $this;
  118.     }
  119. }