vendor/crehler/image-booster/src/Subscriber/WatermarkEventsSubscriber.php line 64

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\ImageBooster\Subscriber;
  3. use Crehler\ImageBooster\Enum\MediaBoosterTypeEnum;
  4. use Crehler\ImageBooster\Event\WatermarkImageGeneratedEvent;
  5. use Crehler\ImageBooster\Exception\WatermarkAdditionalExtensionException;
  6. use Crehler\ImageBooster\Service\Generator\CavifGenerator;
  7. use Crehler\ImageBooster\Service\Generator\PhpGenerator;
  8. use Crehler\ImageBooster\Service\Generator\WebPGeneratorInterface;
  9. use Crehler\ImageBooster\Service\ImageFileSystemSaver;
  10. use Crehler\ImageBooster\Service\MediaBoosterService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class WatermarkEventsSubscriber implements EventSubscriberInterface
  13. {
  14.     private PhpGenerator $webpGenerator;
  15.     private ImageFileSystemSaver $fileSystemSaver;
  16.     private MediaBoosterService $mediaBoosterService;
  17.     private CavifGenerator $cavifGenerator;
  18.     const TARGET_QUALITY 65;
  19.     public function __construct(
  20.         PhpGenerator $webPGenerator,
  21.         CavifGenerator $cavifGenerator,
  22.         ImageFileSystemSaver $fileSystemSaver,
  23.         MediaBoosterService $mediaBoosterService
  24.     )
  25.     {
  26.         $this->webpGenerator $webPGenerator;
  27.         $this->cavifGenerator $cavifGenerator;
  28.         $this->fileSystemSaver $fileSystemSaver;
  29.         $this->mediaBoosterService $mediaBoosterService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             WatermarkImageGeneratedEvent::class => [['generateWebp'1], ['generateAvif'2]]
  35.         ];
  36.     }
  37.     public function generateWebp(WatermarkImageGeneratedEvent $event): void
  38.     {
  39.         $width imagesx($event->getImage());
  40.         $height imagesx($event->getImage());
  41.         $content $this->webpGenerator->generate($event->getImage(), self::TARGET_QUALITY);
  42.         $imageData $this->fileSystemSaver->saveImageToPath($content$event->getPath(), 'webp'$width$height);
  43.         if (!$imageData) {
  44.             throw new WatermarkAdditionalExtensionException();
  45.         }
  46.         $this->mediaBoosterService->addWatermarkRecord(
  47.             $event->getMediaEntity(),
  48.             ($event->getType() === WatermarkImageGeneratedEvent::TYPE_MAIN)?
  49.                 MediaBoosterTypeEnum::WATERMARK_TYPE_WEBP:MediaBoosterTypeEnum::WATERMARK_TYPE_WEBP_THUMB,
  50.             $imageData
  51.         );
  52.     }
  53.     public function generateAvif(WatermarkImageGeneratedEvent $event): void
  54.     {
  55.         $width imagesx($event->getImage());
  56.         $height imagesx($event->getImage());
  57.         $path sprintf("%s.%s"$event->getPath(), $event->getMediaEntity()->getFileExtension());
  58.         $resultPath $this->cavifGenerator->generateAvif($path$event->getPath(), self::TARGET_QUALITY);
  59.         $imageData $this->fileSystemSaver->saveImageToPath(null$event->getPath(), 'avif'$width$height$resultPath);
  60.         if (!$imageData) {
  61.             throw new WatermarkAdditionalExtensionException();
  62.         }
  63.         $this->mediaBoosterService->addWatermarkRecord(
  64.             $event->getMediaEntity(),
  65.             ($event->getType() === WatermarkImageGeneratedEvent::TYPE_MAIN)?
  66.                 MediaBoosterTypeEnum::WATERMARK_TYPE_AVIF:MediaBoosterTypeEnum::WATERMARK_TYPE_AVIF_THUMB,
  67.             $imageData
  68.         );
  69.     }
  70. }