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
+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;
}
}
}