Skip to content

Commit

Permalink
Better icons/screenshots processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Jan 21, 2024
1 parent 1ad6d2c commit 088cdfa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Command/CreateIconsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/Command/CreateScreenshotCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 088cdfa

Please sign in to comment.