src/EventListener/MediaObjectCreatedListener.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\MediaObject;
  5. use App\Entity\Resize;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Event\ViewEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Intervention\Image\ImageManager;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use App\Service\ImageResize;
  15. class MediaObjectCreatedListener implements EventSubscriberInterface
  16. {
  17.     private EntityManagerInterface $entityManager;
  18.     private $kernel;
  19.     private $imageResize;
  20.     public function __construct(EntityManagerInterface $entityManagerKernelInterface $kernelImageResize $imageResize)
  21.     {
  22.         $this->entityManager $entityManager;
  23.         $this->kernel $kernel;
  24.         $this->imageResize $imageResize;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => ['saveMediaObject'EventPriorities::POST_WRITE],
  30.         ];
  31.     }
  32.     public function saveMediaObject(ViewEvent $event)
  33.     {
  34.         $mainMedia $event->getControllerResult();
  35.         $method $event->getRequest()->getMethod();
  36.         if (!$mainMedia instanceof MediaObject) {
  37.             return;
  38.         }
  39.         // echo $mainMedia->getId();
  40.         // echo $mainMedia->filePath;
  41.         $year date('Y');
  42.         $month date('m');
  43.         $documentRoot $this->kernel->getProjectDir() . '/public/media';
  44.         $path $documentRoot "/$year/$month/";
  45.         $name $mainMedia->filePath;
  46.         $uri $path $name;
  47.         if ($this->isImage($uri)) {
  48.             $sizes = [90230700];
  49.             $manager = new ImageManager(
  50.                 new \Intervention\Image\Drivers\Gd\Driver() 
  51.             );
  52.             foreach($sizes as $s){
  53.                 $image $manager->read($uri);
  54.                 $image->scale(width$s);
  55.                 $newName "{$s}_px_" $name;
  56.                 $image->save($path $newName );
  57.                 $newMedia = new MediaObject();
  58.                 $newMedia->filePath $newName;
  59.                 $newMedia->setName("resize-$s");
  60.                 $newMedia->setSize($s);
  61.                 $newMedia->setResizeImgParent($mainMedia);
  62.                 $this->entityManager->persist($newMedia);
  63.                 $this->entityManager->flush();
  64.                
  65.             }
  66.         }
  67.         // die;
  68.        
  69.     }
  70.     
  71.     
  72.     private function isImage($file)
  73.     {
  74.         $allowedTypes = [
  75.             IMAGETYPE_GIF,
  76.             IMAGETYPE_JPEG,
  77.             IMAGETYPE_PNG,
  78.             IMAGETYPE_WEBP,
  79.             // Add more as needed
  80.         ];
  81.         $imageInfo = @getimagesize($file);
  82.         if ($imageInfo && in_array($imageInfo[2], $allowedTypes)) {
  83.             return true;
  84.         }
  85.     
  86.         return false;
  87.     }
  88. }