|
|
@ -5,21 +5,43 @@ namespace App\Event; |
|
|
|
|
|
|
|
use App\Entity\Pokemon; |
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException; |
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
|
|
|
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
|
|
use Vich\UploaderBundle\Event\Event; |
|
|
|
use Vich\UploaderBundle\Event\Events; |
|
|
|
use Liip\ImagineBundle\Service\FilterService; |
|
|
|
|
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
|
|
|
|
|
|
class ResizeImageOnUploadSubscriber implements EventSubscriberInterface |
|
|
|
{ |
|
|
|
/** |
|
|
|
* @var FilterService |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private $filterService; |
|
|
|
|
|
|
|
public function __construct(FilterService $filterService) |
|
|
|
/** |
|
|
|
* @var KernelInterface $appKernel |
|
|
|
*/ |
|
|
|
private $appKernel; |
|
|
|
|
|
|
|
/** |
|
|
|
* @var EntityManagerInterface |
|
|
|
*/ |
|
|
|
protected $entityManager; |
|
|
|
|
|
|
|
public function __construct(FilterService $filterService, KernelInterface $appKernel, EntityManagerInterface $entityManager) |
|
|
|
{ |
|
|
|
$this->filterService = $filterService; |
|
|
|
$this->appKernel = $appKernel; |
|
|
|
$this->entityManager = $entityManager; |
|
|
|
} |
|
|
|
|
|
|
|
public static function getSubscribedEvents(): array |
|
|
@ -33,14 +55,52 @@ class ResizeImageOnUploadSubscriber implements EventSubscriberInterface |
|
|
|
{ |
|
|
|
$object = $event->getObject(); |
|
|
|
if ($object instanceof Pokemon && $object->getImageFile()) { |
|
|
|
// TODO : https://github.com/dustin10/VichUploaderBundle/issues/119 |
|
|
|
// TODO : install GD |
|
|
|
//$thumbnailPath = $this->filterService->getUrlOfFilteredImage($object->getImagePath(), 'some_imagine_filter'); |
|
|
|
// maybe do something with `$thumbnailPath`... |
|
|
|
} |
|
|
|
// Help @: https://github.com/dustin10/VichUploaderBundle/issues/119 |
|
|
|
|
|
|
|
$projectRoot = $this->appKernel->getProjectDir(); |
|
|
|
|
|
|
|
} |
|
|
|
// Création d'une image modifiée en cache |
|
|
|
$thumbnailPath = $this->filterService->getUrlOfFilteredImage("images/pokemons/" . $object->getImageName(), 'my_fixed_filter'); |
|
|
|
$thumbnailInternalPath = parse_url($thumbnailPath)['path']; |
|
|
|
|
|
|
|
// Tampon pour les infos de l'ancienne image |
|
|
|
$originalFile = $object->getImageFile(); |
|
|
|
$originalFilename = $object->getImageName(); |
|
|
|
|
|
|
|
// On supprime l'ancienne image |
|
|
|
$filesystem = new Filesystem(); |
|
|
|
try { |
|
|
|
$filesystem->remove($originalFile->getRealPath()); |
|
|
|
} catch (IOExceptionInterface $exception) { |
|
|
|
echo "An error occurred while deleting your file at " . $exception->getPath(); |
|
|
|
} |
|
|
|
|
|
|
|
// On deplace le fichier dans le repertoire /images/pokemons : |
|
|
|
$thePathToGetCachedImage = $projectRoot . '/public/' . $thumbnailInternalPath; |
|
|
|
$newFileSymfo = new File($thePathToGetCachedImage); |
|
|
|
$pokemonImagesDirectory = $projectRoot . '/public/images/pokemons'; |
|
|
|
|
|
|
|
try { |
|
|
|
$newFileSymfo->move( |
|
|
|
$pokemonImagesDirectory, |
|
|
|
$originalFilename |
|
|
|
); |
|
|
|
} catch (FileException $e) { |
|
|
|
// ... handle exception if something happens during file upload |
|
|
|
dd($e); |
|
|
|
} |
|
|
|
|
|
|
|
// On attribue la nouvelle image dans le repertoire à notre pokemon |
|
|
|
$newUploadedFile = new UploadedFile($pokemonImagesDirectory . '/' . $originalFilename, $originalFilename); |
|
|
|
|
|
|
|
$object->setImageFile($newUploadedFile); |
|
|
|
$object->setImageName($newUploadedFile->getClientOriginalName()); |
|
|
|
$object->setImageSize($newUploadedFile->getSize()); |
|
|
|
$this->entityManager->persist($object); |
|
|
|
|
|
|
|
// $this->entityManager->flush(); // Todo: flush fait fail l'upload, pourquoi ? (poser la question à Simon) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |