feat: generate image thumbnails after deployment

This commit is contained in:
Björn Fromme
2024-11-29 13:16:52 +01:00
parent 1c55b4785c
commit 30111df408
3 changed files with 76 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
<?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;
}
}