src/EventListener/MediaObjectCreatedListener.php line 41

  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. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. class MediaObjectCreatedListener implements EventSubscriberInterface
  17. {
  18.     private EntityManagerInterface $entityManager;
  19.     private $kernel;
  20.     private $imageResize;
  21.     private $other_user '';
  22.     private $other_host '';
  23.     private $other_port '';
  24.     private $other_pass '';
  25.     private $other_name '';
  26.     private $my_user '';
  27.     private $my_host '';
  28.     private $my_port '';
  29.     private $my_pass '';
  30.     private $my_name '';
  31.     private $params;
  32.     public function __construct(EntityManagerInterface $entityManagerKernelInterface $kernelImageResize $imageResizeParameterBagInterface $params )
  33.     {
  34.         $this->entityManager $entityManager;
  35.         $this->kernel $kernel;
  36.         $this->imageResize $imageResize;
  37.         $this->params $params;
  38.         /** стороння база */
  39.         $this->other_user$this->params->get('OTHER_DB_user');
  40.         $this->other_host$this->params->get('OTHER_DB_host');
  41.         $this->other_port$this->params->get('OTHER_DB_port');
  42.         $this->other_pass$this->params->get('OTHER_DB_pass');
  43.         $this->other_name$this->params->get('OTHER_DB_name');
  44.         /** моя база */
  45.         // $this->my_user= $this->params->get('MY_DB_user');
  46.         // $this->my_host= $this->params->get('MY_DB_host');
  47.         // $this->my_port= $this->params->get('MY_DB_port');
  48.         // $this->my_pass= $this->params->get('MY_DB_pass');
  49.         // $this->my_name= $this->params->get('MY_DB_name');
  50.     }
  51.     public static function getSubscribedEvents()
  52.     {
  53.         return [
  54.             KernelEvents::VIEW => ['saveMediaObject'EventPriorities::POST_WRITE],
  55.         ];
  56.     }
  57.     public function saveMediaObject(ViewEvent $event)
  58.     {
  59.         $mainMedia $event->getControllerResult();
  60.         $method $event->getRequest()->getMethod();
  61.         if (!$mainMedia instanceof MediaObject) {
  62.             return;
  63.         }
  64.         // echo $mainMedia->getId();
  65.         // echo $mainMedia->filePath;
  66.         $year date('Y');
  67.         $month date('m');
  68.         $documentRoot $this->kernel->getProjectDir() . '/public/media';
  69.         $path $documentRoot "/$year/$month/";
  70.         $name $mainMedia->filePath;
  71.         $uri $path $name;
  72.        
  73.         $this->dublicate([
  74.             'id' => $mainMedia->getId(),
  75.             'file_path' => $mainMedia->filePath,
  76.             'type' => $mainMedia->getType(),
  77.             'name' => $mainMedia->getName(),
  78.             'upload_timestamp' => $mainMedia->getUploadTimestamp()->format('Y-m-d H:i:s'),  
  79.             'size' => null,  
  80.             'resize_img_parent_id' => null,  
  81.         ]);
  82.      
  83.         if ( $mainMedia->getResizeImgParent() == null AND $this->isImage($uri)) {
  84.             $sizes = [902307001200];
  85.             $manager = new ImageManager(
  86.                 new \Intervention\Image\Drivers\Gd\Driver() 
  87.             );
  88.             foreach($sizes as $s){
  89.                 $image $manager->read($uri);
  90.                 $image->scale$s);
  91.                 $newName "{$s}_px_" $name;
  92.                 $image->save($path $newName );
  93.                 $newMedia = new MediaObject();
  94.                 $newMedia->filePath $newName;
  95.                 $newMedia->setName("resize-$s");
  96.                 $newMedia->setSize($s);
  97.                 $newMedia->setResizeImgParent($mainMedia);
  98.                 $this->entityManager->persist($newMedia);
  99.                 $this->entityManager->flush();
  100.                 $this->dublicate([
  101.                     'id' => $newMedia->getId(),
  102.                     'file_path' => $newMedia->filePath,
  103.                     'type' => $newMedia->getType(),
  104.                     'name' => $newMedia->getName(),
  105.                     'upload_timestamp' => $newMedia->getUploadTimestamp()->format('Y-m-d H:i:s'),  
  106.                     'size' => $newMedia->getSize(),  
  107.                     'resize_img_parent_id' =>  $mainMedia->getId(),  
  108.                 ]);
  109.                
  110.             }
  111.         }
  112.         // die;
  113.        
  114.     }
  115.     
  116.     private function dublicate($data){
  117.         $dsn "pgsql:host={$this->other_host};port={$this->other_port};dbname={$this->other_name};";
  118.         // 3. Опції
  119.         $options = [
  120.             \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
  121.             \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
  122.             \PDO::ATTR_EMULATE_PREPARES   => false,
  123.         ];
  124.       
  125.             // 4. Підключення
  126.             $pdo = new \PDO($dsn$this->other_user$this->other_pass$options);
  127.             // 5. INSERT-запит
  128.             $sql "INSERT INTO media_object (
  129.                 id,
  130.                 file_path, 
  131.                 type,
  132.                 name,
  133.                 upload_timestamp,
  134.                 \"size\",
  135.                 resize_img_parent_id
  136.             ) VALUES (
  137.             :id,
  138.             :file_path, 
  139.             :type,
  140.             :name,
  141.             :upload_timestamp,
  142.             :size,
  143.             :resize_img_parent_id
  144.             )";
  145.             $stmt $pdo->prepare($sql);
  146.             $stmt->execute([
  147.                 'id' => $data['id'],
  148.                 'file_path' => $data['file_path'],
  149.                 'type' => $data['type'],
  150.                 'name' => $data['name'],
  151.                 'upload_timestamp' => $data['upload_timestamp'],
  152.                 'size' => $data['size'],
  153.                 'resize_img_parent_id' => $data['resize_img_parent_id'],
  154.             ]);
  155.     }
  156.     
  157.     private function isImage($file)
  158.     {
  159.         $allowedTypes = [
  160.             IMAGETYPE_GIF,
  161.             IMAGETYPE_JPEG,
  162.             IMAGETYPE_PNG,
  163.             IMAGETYPE_WEBP,
  164.             // Add more as needed
  165.         ];
  166.         $imageInfo = @getimagesize($file);
  167.         if ($imageInfo && in_array($imageInfo[2], $allowedTypes)) {
  168.             return true;
  169.         }
  170.     
  171.         return false;
  172.     }
  173. }