From 088cdfa84f8ff4e483a17b19d1424f221f75a124 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Sun, 21 Jan 2024 11:34:46 +0100 Subject: [PATCH] Better icons/screenshots processing --- src/Command/CreateIconsCommand.php | 9 ++++++++- src/Command/CreateScreenshotCommand.php | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Command/CreateIconsCommand.php b/src/Command/CreateIconsCommand.php index 2cb102f..36c056a 100644 --- a/src/Command/CreateIconsCommand.php +++ b/src/Command/CreateIconsCommand.php @@ -48,7 +48,14 @@ protected function configure(): void sprintf('%s/assets/icons/', $this->projectDir) ); $this->addArgument('filename', InputArgument::OPTIONAL, 'The output directory', 'icon'); - $this->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'The format of the icons'); + $this->addOption( + 'format', + 'f', + InputOption::VALUE_OPTIONAL, + 'The format of the icons', + null, + ['png', 'jpg', 'webp'] + ); $this->addArgument( 'sizes', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, diff --git a/src/Command/CreateScreenshotCommand.php b/src/Command/CreateScreenshotCommand.php index 4f27ad3..4c2d20c 100644 --- a/src/Command/CreateScreenshotCommand.php +++ b/src/Command/CreateScreenshotCommand.php @@ -5,6 +5,7 @@ namespace SpomkyLabs\PwaBundle\Command; use Facebook\WebDriver\WebDriverDimension; +use SpomkyLabs\PwaBundle\ImageProcessor\ImageProcessor; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -27,6 +28,7 @@ final class CreateScreenshotCommand extends Command private readonly Client $webClient; public function __construct( + private readonly ImageProcessor $imageProcessor, private readonly Filesystem $filesystem, #[Autowire('%kernel.project_dir%')] private readonly string $projectDir, @@ -64,6 +66,14 @@ protected function configure(): void ); $this->addOption('width', null, InputOption::VALUE_OPTIONAL, 'The width of the screenshot'); $this->addOption('height', null, InputOption::VALUE_OPTIONAL, 'The height of the screenshot'); + $this->addOption( + 'format', + 'f', + InputOption::VALUE_OPTIONAL, + 'The format of the icons', + null, + ['png', 'jpg', 'webp'] + ); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -77,6 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $url = $input->getArgument('url'); $height = $input->getOption('height'); $width = $input->getOption('width'); + $format = $input->getOption('format'); $client = clone $this->webClient; $client->request('GET', $url); @@ -92,6 +103,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->fullscreen(); $client->takeScreenshot($tmpName); + if ($format !== null) { + $data = $this->imageProcessor->process(file_get_contents($tmpName), null, null, $format); + file_put_contents($tmpName, $data); + } + if ($width === null || $height === null) { + ['width' => $width, 'height' => $height] = $this->imageProcessor->getSizes(file_get_contents($tmpName)); + } + $mime = MimeTypes::getDefault(); $mimeType = $mime->guessMimeType($tmpName); $extensions = $mime->getExtensions($mimeType);