src/EventListener/UploadedImagesListener.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Service\ImageResizer;
  4. use Symfony\Component\HttpFoundation\File\File;
  5. use Vich\UploaderBundle\Event\Event;
  6. class UploadedImagesListener
  7. {
  8.     public function onVichUploaderPostUpload(Event $event)
  9.     {
  10.         $mapping $event->getMapping();
  11.         $object $event->getObject();
  12.         $file $mapping->getFile($object);
  13.         $ext $file->guessExtension();
  14.         if (!in_array($ext, ['jpg''png''jpeg'])) {
  15.             return;
  16.         }
  17.         $filePath $file->getPathname();
  18.         $name str_replace('.'''uniqid(''true)).'.'.$ext;
  19.         $newFilePath $file->getPath().'/'.$name;
  20.         if (!ImageResizer::resizeImage($filePath$newFilePath)) {
  21.             return;
  22.         }
  23.         // Set new file info
  24.         $newFile = new File($newFilePathfalse);
  25.         $mapping->setFileName($object$name);
  26.         $mapping->writeProperty($object'size'$newFile->getSize());
  27.         $dimensions = @getimagesize($newFile);
  28.         $mapping->writeProperty($object'dimensions'array_splice($dimensions02));
  29.     }
  30. }