vendor/kematjaya/upload-bundle/src/EventSubscriber/ImageOptimationSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  4.  * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
  5.  */
  6. namespace Kematjaya\UploadBundle\EventSubscriber;
  7. use Kematjaya\UploadBundle\Event\PostUploadFileEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. /**
  12.  * Description of ImageOptimationSUbscriber
  13.  *
  14.  * @author apple
  15.  */
  16. class ImageOptimationSubscriber implements EventSubscriberInterface 
  17. {
  18.     /**
  19.      * 
  20.      * @var array
  21.      */
  22.     private $optimizer;
  23.     
  24.     public function __construct(ParameterBagInterface $parameterBag
  25.     {
  26.         $this->optimizer $parameterBag->get("upload")["optimizer"]["image"];
  27.     }
  28.     
  29.     public static function getSubscribedEvents():array
  30.     {
  31.         return [
  32.             PostUploadFileEvent::EVENT_NAME => "optimation"
  33.         ];
  34.     }
  35.     public function optimation(PostUploadFileEvent $event):void
  36.     {
  37.         $uploadedFile $event->getFile();
  38.         $allowedImages = ['jpeg''gif''jpg''png'];
  39.         if (!in_array($uploadedFile->getExtension(), $allowedImages)) {
  40.             
  41.             return;
  42.         }
  43.         
  44.         $mimeInfo getimagesize($uploadedFile->getRealPath());
  45.         $imageMimeType $mimeInfo['mime'];
  46.         $optimizedFile $this->compressImage($imageMimeType$uploadedFile);
  47.         
  48.         if (true === $this->optimizer["remove_origin"]) {
  49.             unlink($uploadedFile->getRealPath());
  50.         }
  51.         
  52.         $event->setFile($optimizedFile);
  53.     }
  54.     
  55.     protected function compressImage(string $mimeTypeFile $originalFile): File
  56.     {
  57.         $quality $this->optimizer["quality"];
  58.         switch ($mimeType) {
  59.             case 'image/png':
  60.                 $img imagecreatefrompng($originalFile->getPathname());
  61.                 break;
  62.             case 'image/jpeg':
  63.                 $img imagecreatefromjpeg($originalFile->getPathname());
  64.                 break;
  65.             case 'image/gif':
  66.                 $img imagecreatefromgif($originalFile->getPathname());
  67.                 break;
  68.             default:
  69.                 $img imagecreatefromjpeg($originalFile->getPathname());
  70.         }
  71.         
  72.         $name str_replace("." $originalFile->getExtension(), ""$originalFile->getFilename());
  73.         $newImagePath sprintf("%s/%s-optimized.jpg"$originalFile->getPath(), $name);
  74.         if (false === imagejpeg($img$newImagePath$quality)) {
  75.             throw new \Exception("failed to optimized image.");
  76.         }
  77.         
  78.         return new File($newImagePath);
  79.     }
  80. }