This commit is contained in:
Jürgen Mummert
2025-12-23 12:31:23 +01:00
parent 093ac084bb
commit 22c73baa5c
2 changed files with 71 additions and 1 deletions
+11 -1
View File
@@ -2,8 +2,14 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use MummertMedia\ContaoMeilisearchBundle\Service\MeilisearchImageHelper;
class IndexPageListener
{
public function __construct(
private readonly MeilisearchImageHelper $imageHelper,
) {}
public function onIndexPage(string $content, array &$data, array &$set): void
{
// Marker vorhanden?
@@ -72,7 +78,11 @@ class IndexPageListener
$parsed['custom']['searchimage'] ?? null;
if (is_string($image) && $image !== '') {
$set['imagepath'] = trim($image);
$path = $this->imageHelper->getImagePathFromUuid($image);
if ($path !== null) {
$set['imagepath'] = $path;
}
}
/*
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace MummertMedia\ContaoMeilisearchBundle\Service;
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,
) {}
/**
* 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) {
return null;
}
}
}