Skip to content

Commit

Permalink
Merge branch 'release/0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisdeBest committed Feb 9, 2022
2 parents 2f0e020 + 6b11fb0 commit 092336b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 107 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
"symfony/validator": "^5.0 || ^6.0",
"symfony/orm-pack": "^2.1",
"twig/twig": "^2.0 || ^3.0",
"ext-fileinfo": "*"
},
"ext-fileinfo": "*"
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"symfony/framework-bundle": "^5.0 || ^6.0",
"symfony/phpunit-bridge": "^5.0 || ^6.0"
"roave/security-advisories": "dev-latest",
"symfony/framework-bundle": "^5.0 || ^6.0",
"symfony/phpunit-bridge": "^5.0 || ^6.0"
},
"autoload": {
"psr-4": {
Expand Down
203 changes: 101 additions & 102 deletions src/Command/WebPConversionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,106 +17,105 @@

class WebPConversionCommand extends Command
{
protected static $defaultName = 'codebuds:webp:convert';

protected const CONVERTABLE_MIME_TYPES = [
'image/jpg',
'image/jpeg',
'image/png',
'image/gif',
];

public function __construct(private int $quality, private string $projectDir)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Generate webps in directories')
->addArgument('directories', InputArgument::IS_ARRAY, 'Directories for webP generation')
->addOption('create', null, InputOption::VALUE_NONE, 'Generate new files')
->addOption('force', null, InputOption::VALUE_NONE, 'Recreate all the files')
->addOption('quality', null, InputOption::VALUE_OPTIONAL, 'Set webp generation quality', $this->quality)
->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'Add a filename suffix', '');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$directories = $input->getArgument('directories');

if (!$directories) {
$io->error('no directories passed');

return 1;
}

$stopwatch = new Stopwatch();

$stopwatch->start('WebP_transforms');

$images = [];

array_walk($directories, function ($directory) use (&$images) {
$fullPath = $directory[0] === '/' ?: "{$this->projectDir}/{$directory}";
$images = $this->scanDirs($fullPath);
});

if ($input->getOption('create')) {
$progressBar = new ProgressBar($output, count($images));
$progressBar->start();

$errors = [];
array_walk($images, static function ($image) use ($progressBar, &$errors, $input) {
$progressBar->advance();
try {
$options = [
'quality' => (int)$input->getOption('quality'),
'force' => $input->getOption('force'),
'saveFile' => $input->getOption('create'),
'filenameSuffix' => $input->getOption('suffix'),
];
WebPConverter::createWebPImage($image["path"], $options);
} catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
});
$progressBar->finish();

if ($errors) {
$io->error($errors);
}
} else {
$io->text(count($images) . " images found, add --create to start webP generation");
}

$event = $stopwatch->stop('WebP_transforms');
$io->text("Time : " . $event);

return 0;
}

private function scanDirs(string $fullPath): array
{
$finder = new Finder();
$elements = [];
$finder->files()->in($fullPath);
$mimeTypeFinder = new FileinfoMimeTypeGuesser();
if ($finder->hasResults()) {
foreach ($finder as $file) {
$type = $mimeTypeFinder->guessMimeType($file->getPathname());
if (!in_array($type, self::CONVERTABLE_MIME_TYPES, true)){
continue;
}
$elements[] = [
"name" => $file->getFilenameWithoutExtension(),
"extension" => $file->getExtension(),
"path" => $file
];
}
}
return $elements;
}
protected static $defaultName = 'codebuds:webp:convert';

protected const CONVERTABLE_MIME_TYPES = [
'image/jpeg',
'image/png',
'image/gif',
];

public function __construct(private int $quality, private string $projectDir)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setDescription('Generate webps in directories')
->addArgument('directories', InputArgument::IS_ARRAY, 'Directories for webP generation')
->addOption('create', null, InputOption::VALUE_NONE, 'Generate new files')
->addOption('force', null, InputOption::VALUE_NONE, 'Recreate all the files')
->addOption('quality', null, InputOption::VALUE_OPTIONAL, 'Set webp generation quality', $this->quality)
->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'Add a filename suffix', '');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$directories = $input->getArgument('directories');

if (!$directories) {
$io->error('no directories passed');

return 1;
}

$stopwatch = new Stopwatch();

$stopwatch->start('WebP_transforms');

$images = [];

array_walk($directories, function ($directory) use (&$images) {
$fullPath = $directory[0] === '/' ?: "{$this->projectDir}/{$directory}";
$images = $this->scanDirs($fullPath);
});

if ($input->getOption('create')) {
$progressBar = new ProgressBar($output, count($images));
$progressBar->start();

$errors = [];
array_walk($images, static function ($image) use ($progressBar, &$errors, $input) {
$progressBar->advance();
try {
$options = [
'quality' => (int)$input->getOption('quality'),
'force' => $input->getOption('force'),
'saveFile' => $input->getOption('create'),
'filenameSuffix' => $input->getOption('suffix'),
];
WebPConverter::createWebPImage($image["path"], $options);
} catch (Exception $exception) {
$errors[] = $exception->getMessage();
}
});
$progressBar->finish();

if ($errors) {
$io->error($errors);
}
} else {
$io->text(count($images) . " images found, add --create to start webP generation");
}

$event = $stopwatch->stop('WebP_transforms');
$io->text("Time : " . $event);

return 0;
}

private function scanDirs(string $fullPath): array
{
$finder = new Finder();
$elements = [];
$finder->files()->in($fullPath);
$mimeTypeFinder = new FileinfoMimeTypeGuesser();
if ($finder->hasResults()) {
foreach ($finder as $file) {
$type = $mimeTypeFinder->guessMimeType($file->getPathname());
if (!in_array($type, self::CONVERTABLE_MIME_TYPES, true)) {
continue;
}
$elements[] = [
"name" => $file->getFilenameWithoutExtension(),
"extension" => $file->getExtension(),
"path" => $file
];
}
}
return $elements;
}
}

0 comments on commit 092336b

Please sign in to comment.