This commit is contained in:
Jürgen Mummert
2025-12-23 12:33:39 +01:00
parent 22c73baa5c
commit 22c6a3e0b6
2 changed files with 32 additions and 44 deletions
+4 -1
View File
@@ -8,4 +8,7 @@ services:
MummertMedia\ContaoMeilisearchBundle\EventListener\IndexPageListener:
tags:
- { name: contao.hook, hook: indexPage, method: onIndexPage }
- { name: contao.hook, hook: indexPage, method: onIndexPage }
Contao\CoreBundle\Filesystem\FilesystemInterface:
alias: filesStorage
+28 -43
View File
@@ -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;
}
}