From 22c6a3e0b6c84fb3f66e4a97ef5f3b36c578a6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Mummert?= Date: Tue, 23 Dec 2025 12:33:39 +0100 Subject: [PATCH] Bugfix --- src/Resources/config/services.yaml | 5 +- src/Service/MeilisearchImageHelper.php | 71 ++++++++++---------------- 2 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 00ff71d..822839d 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -8,4 +8,7 @@ services: MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener: tags: - - { name: contao.hook, hook: indexPage, method: onIndexPage } \ No newline at end of file + - { name: contao.hook, hook: indexPage, method: onIndexPage } + + Contao\CoreBundle\Filesystem\FilesystemInterface: + alias: filesStorage \ No newline at end of file diff --git a/src/Service/MeilisearchImageHelper.php b/src/Service/MeilisearchImageHelper.php index 4ebfff5..5166dea 100644 --- a/src/Service/MeilisearchImageHelper.php +++ b/src/Service/MeilisearchImageHelper.php @@ -2,59 +2,44 @@ namespace MummertMedia\ContaoMeilisearchBundle\Service; +use Contao\CoreBundle\Filesystem\FilesystemInterface; +use Contao\Image\Studio; use Contao\Config; -use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface; -use Contao\CoreBundle\Image\Studio\Studio; class MeilisearchImageHelper { public function __construct( - private readonly Studio $studio, - private readonly VirtualFilesystemInterface $filesystem, + private readonly FilesystemInterface $filesStorage, + private readonly Studio $imageStudio, ) {} - /** - * UUID → finaler Bildpfad für tl_search.imagepath - */ public function getImagePathFromUuid(string $uuid): ?string { - try { - $file = $this->filesystem->read($uuid); - - if ($file === null || !$file->isFile()) { - return null; - } - - $path = $file->getPath(); - - // ------------------------- - // SVG → niemals skalieren - // ------------------------- - if (str_ends_with(strtolower($path), '.svg')) { - return '/' . ltrim($path, '/'); - } - - // ------------------------- - // Rasterbild → Image Studio - // ------------------------- - $sizeId = Config::get('meilisearch_imagesize'); - - if (!$sizeId) { - return '/' . ltrim($path, '/'); - } - - $figure = $this->studio - ->createFigureBuilder() - ->fromFile($file) - ->setSize($sizeId) - ->build(); - - $image = $figure->getImage(); - - return $image?->getUrl(); - - } catch (\Throwable) { + // UUID → Datei + if (!$this->filesStorage->fileExists($uuid)) { return null; } + + $file = $this->filesStorage->get($uuid); + $path = $file->getPath(); + + // SVG: nicht skalieren + if (str_ends_with(strtolower($path), '.svg')) { + return '/' . ltrim($path, '/'); + } + + // Bildgröße aus tl_settings + $sizeId = (int) Config::get('meilisearch_imagesize'); + if ($sizeId <= 0) { + return '/' . ltrim($path, '/'); + } + + // Bild über Image Studio erzeugen + $figure = $this->imageStudio + ->createFigure($uuid) + ->setSize($sizeId) + ->build(); + + return $figure?->getImage()?->getSrc() ?? null; } } \ No newline at end of file