ERP/MediaBundle/EventSubscriber/MediaObjectCreatedSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace ERP\MediaBundle\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use ERP\MediaBundle\Entity\MediaObject;
  5. use ERP\MediaBundle\Util\Image;
  6. use Intervention\Image\ImageManager;
  7. use Spatie\ImageOptimizer\OptimizerChainFactory;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Vich\UploaderBundle\Storage\StorageInterface;
  12. class MediaObjectCreatedSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ImageManager
  16.      */
  17.     private $imageManager;
  18.     /**
  19.      * @var StorageInterface
  20.      */
  21.     private $storage;
  22.     /**
  23.      * @var \ERP\MediaBundle\Services\Image
  24.      */
  25.     private $imageHelper;
  26.     public function __construct(
  27.         StorageInterface $storage,
  28.         \ERP\MediaBundle\Services\Image $imageHelper
  29.     ) {
  30.         $this->storage $storage;
  31.         $this->imageManager = new ImageManager();
  32.         $this->imageHelper $imageHelper;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             KernelEvents::VIEW => ['mediaObjectCreated'EventPriorities::POST_WRITE],
  38.         ];
  39.     }
  40.     public function mediaObjectCreated(RequestEvent $event)
  41.     {
  42.         $request $event->getRequest();
  43.         if ('api_media_objects_post_collection' !== $request->get('_route')) {
  44.             return;
  45.         }
  46.         /** @var MediaObject $mediaObject */
  47.         $mediaObject $event->getControllerResult();
  48.         if (!$mediaObject instanceof MediaObject) {
  49.             return;
  50.         }
  51.         if (!in_array($mediaObject->file->getMimeType(), Image::IMAGE_MIME_TYPES)) {
  52.             return;
  53.         }
  54.         $optimizerChain OptimizerChainFactory::create();
  55.         $originalImagePath $this->storage->resolvePath($mediaObject'file');
  56.         $originalImage $this->imageManager->make($originalImagePath);
  57.         $quality $mediaObject->getOptions()['quality'] ?? \ERP\MediaBundle\Services\Image::QUALITY;
  58.         $originalImage->save(null$quality);
  59.         $optimizerChain->optimize($originalImagePath);
  60.         $modifiedImagePath $this->storage->resolvePath($mediaObject'fileModified');
  61.         if (null === $mediaObject->getParent() && $modifiedImagePath) {
  62.             $this->imageHelper->createCommonSizes($modifiedImagePath$mediaObject);
  63.         } elseif ((null === $mediaObject->getParent()) && $originalImagePath) {
  64.             $this->imageHelper->createCommonSizes($originalImagePath$mediaObject);
  65.         }
  66.         if ($modifiedImagePath) {
  67.             $optimizerChain->optimize($modifiedImagePath);
  68.         }
  69.     }
  70. }