src/EventSubscriber/FotoColaboradorSubscriber.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Imagick;
  4. use Vich\UploaderBundle\Event\Event;
  5. class FotoColaboradorSubscriber
  6. {
  7.     const LIMIT_PHOTO_SIZE 200000;
  8.     public function onVichUploaderPreUpload(Event $event)
  9.     {
  10.         $object $event->getObject();
  11.         $fileName $object->getFotoFile()->getPathName();
  12.         $img = new Imagick($fileName);
  13.         if ($img->getImageHeight() > $img->getImageWidth()) {
  14.             if ($img->getImageWidth() > 728) {
  15.                 $img->adaptiveResizeImage(0728);
  16.             }
  17.         } else {
  18.             if ($img->getImageHeight() > 1024) {
  19.                 $img->adaptiveResizeImage(10240);
  20.             }
  21.         }
  22.         $img->writeImage($fileName);
  23.     }
  24.     public function onVichUploaderPostUpload(Event $event)
  25.     {
  26.         $object $event->getObject();
  27.         $img = new Imagick('files/foto_persona/' $object->getFoto());
  28.         $originalImg = clone $img;
  29.         $quality $img->getImageCompressionQuality() - 3;// (ceil(100 / (($img->getImageLength() * 100 / $img->getImageCompressionQuality()) / self::LIMIT_PHOTO_SIZE)));
  30.         while ($img->getImageLength() > self::LIMIT_PHOTO_SIZE) {
  31.             $originalImg->setImageCompressionQuality($quality);
  32.             $originalImg->writeImage('files/foto_persona/' $object->getFoto());
  33.             $img = new Imagick('files/foto_persona/' $object->getFoto());
  34.             $quality $quality 3;
  35.         }
  36.     }
  37. }