Files
myep-team/src/Command/GenerateThumbnailsCommand.php
T
2025-08-21 14:20:43 +02:00

67 lines
2.0 KiB
PHP

<?php
namespace App\Command;
use App\Entity\Teamer;
use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Service\FilterService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:generate-thumbnails',
description: 'Generates thumbnails of profile images',
)]
class GenerateThumbnailsCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly CacheManager $cacheManager,
private readonly FilterService $filterService,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$images = $this
->entityManager
->getRepository(Teamer::class)
->createQueryBuilder('t')
->select('t.id', 'p.filename')
->innerJoin('t.photo', 'p')
->getQuery()
->getArrayResult()
;
$imagesCount = count($images);
$io->info('Generating thumbnails for '.$imagesCount.' profile images...');
$progressbar = $io->createProgressBar($imagesCount);
$progressbar->start();
foreach ($images as $image) {
try {
$this->filterService->warmUpCache($image['filename'], 'thumbnail');
$this->filterService->warmUpCache($image['filename'], 'profile');
$this->cacheManager->getBrowserPath($image['filename'], 'thumbnail');
$this->cacheManager->getBrowserPath($image['filename'], 'profile');
} catch (\Exception $e) {
}
$progressbar->advance();
}
$progressbar->finish();
return Command::SUCCESS;
}
}