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
+5
View File
@@ -53,6 +53,11 @@ services:
$connection: '@doctrine.dbal.default_connection'
$xmlFilesPath: '%kernel.project_dir%/xmlexport'
App\Command\GenerateThumbnailsCommand:
arguments:
$cacheManager: '@liip_imagine.cache.manager'
$filterService: '@liip_imagine.service.filter'
App\BusProNet\ApiClient:
arguments:
$logger: '@monolog.logger.bpn'
+5
View File
@@ -92,6 +92,7 @@ task('deploy', [
'database:migrate',
'deploy:publish',
'cachetool:clear:opcache',
'deploy:thumbnails',
// 'deploy:stop-workers',
]);
@@ -103,4 +104,8 @@ task('deploy:assets', function () {
runLocally('npm i && npm run build');
});
task('deploy:thumbnails', function () {
run('{{bin/console}} app:generate-thumbnails');
});
after('deploy:failed', 'deploy:unlock');
+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;
}
}