From 6a9bf6c233d344dda4defc427f509cf409196013 Mon Sep 17 00:00:00 2001 From: David Mellen Date: Thu, 5 Sep 2024 09:25:00 +0200 Subject: [PATCH] feat: add crop parameter to asset middleware REHLTYEXT-89 --- Classes/Middleware/AssetMiddleware.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Classes/Middleware/AssetMiddleware.php b/Classes/Middleware/AssetMiddleware.php index 33a4d50..a8f233f 100644 --- a/Classes/Middleware/AssetMiddleware.php +++ b/Classes/Middleware/AssetMiddleware.php @@ -9,6 +9,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; +use TYPO3\CMS\Core\Imaging\ImageManipulation\Area; use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; use TYPO3\CMS\Core\Resource\AbstractFile; use TYPO3\CMS\Core\Resource\ResourceFactory; @@ -79,11 +80,26 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $targetFileExtension === 'svg' ) ) { - $cropVariant = $queryParams['breakpoint'] ?? 'default'; + $cropName = $queryParams['crop'] ?? null; + $breakpoint = $queryParams['breakpoint'] ?? null; + + $cropVariants = array_filter([ + implode('-', array_filter([$cropName, $breakpoint])), + $cropName, + $breakpoint, + ]); $crop = $resource->getProperty('crop') ?? ''; $cropVariantCollection = CropVariantCollection::create($crop); - $cropArea = $cropVariantCollection->getCropArea($cropVariant); + + $cropArea = Area::createEmpty(); + + foreach ($cropVariants as $cropVariant) { + $cropArea = $cropVariantCollection->getCropArea($cropVariant); + if (!$cropArea->isEmpty()) { + break; + } + } // Use default cropVariant if breakpoint cropVariant does not exist if ($cropArea->isEmpty()) {