diff --git a/Classes/Event/Listener/EnrichFileDataEventListener.php b/Classes/Event/Listener/EnrichFileDataEventListener.php index 7b97cae..61817ec 100644 --- a/Classes/Event/Listener/EnrichFileDataEventListener.php +++ b/Classes/Event/Listener/EnrichFileDataEventListener.php @@ -5,6 +5,7 @@ namespace Remind\Headless\Event\Listener; use FriendsOfTYPO3\Headless\Event\EnrichFileDataEvent; +use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; use TYPO3\CMS\Core\Utility\GeneralUtility; class EnrichFileDataEventListener @@ -38,6 +39,24 @@ public function __invoke(EnrichFileDataEvent $event): void } } + /** + * Crop Variants with non-empty crop areas of SVG Images are converted to SVG + * so these images have to be treated different than pure SVGs in frontend + */ + + if ($originalFile->getExtension() === 'svg') { + $crop = $originalFile->getProperty('crop'); + $cropVariantCollection = CropVariantCollection::create($crop); + $cropVariants = empty($crop) ? [] : array_keys(json_decode($crop, true)); + foreach ($cropVariants as $cropVariant) { + $cropArea = $cropVariantCollection->getCropArea((string) $cropVariant); + if (!$cropArea->isEmpty()) { + $properties['extension'] = 'mixed'; + break; + } + } + } + $event->setProperties($properties); } } diff --git a/Classes/Middleware/AssetMiddleware.php b/Classes/Middleware/AssetMiddleware.php index a8f233f..a29b450 100644 --- a/Classes/Middleware/AssetMiddleware.php +++ b/Classes/Middleware/AssetMiddleware.php @@ -72,43 +72,59 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($type === AbstractFile::FILETYPE_IMAGE) { $targetFileExtension = $queryParams['fileExtension'] ?? null; - // Skip processing for SVGs without changing image type - if ( - $resource->getExtension() !== 'svg' || - ( - !$targetFileExtension || - $targetFileExtension === 'svg' - ) - ) { - $cropName = $queryParams['crop'] ?? null; - $breakpoint = $queryParams['breakpoint'] ?? null; + $cropName = $queryParams['crop'] ?? null; + $breakpoint = $queryParams['breakpoint'] ?? null; - $cropVariants = array_filter([ - implode('-', array_filter([$cropName, $breakpoint])), - $cropName, - $breakpoint, - ]); + $cropVariants = array_filter([ + implode('-', array_filter([$cropName, $breakpoint])), + $cropName, + $breakpoint, + ]); - $crop = $resource->getProperty('crop') ?? ''; - $cropVariantCollection = CropVariantCollection::create($crop); + $crop = $resource->getProperty('crop') ?? ''; + $cropVariantCollection = CropVariantCollection::create($crop); - $cropArea = Area::createEmpty(); + $cropArea = Area::createEmpty(); - foreach ($cropVariants as $cropVariant) { - $cropArea = $cropVariantCollection->getCropArea($cropVariant); - if (!$cropArea->isEmpty()) { - break; - } + foreach ($cropVariants as $cropVariant) { + $cropArea = $cropVariantCollection->getCropArea($cropVariant); + if (!$cropArea->isEmpty()) { + break; } + } + + $skipProcessing = false; // Use default cropVariant if breakpoint cropVariant does not exist - if ($cropArea->isEmpty()) { - $cropArea = $cropVariantCollection->getCropArea(); + if ($cropArea->isEmpty()) { + $cropArea = $cropVariantCollection->getCropArea(); + + /** + * Skip processing if cropArea is empty and + * - targetFileExtension is SVG because TYPO3 cannot process SVGs + * - source is SVG and targetFileExtension is not set + * (normally TYPO3 would convert SVGs to PNGs with targetFileExtension not set, + * but there is no need with an empty cropArea) + */ + if ( + $targetFileExtension === 'svg' || + $resource->getExtension() === 'svg' && + !$targetFileExtension + ) { + $skipProcessing = true; } + } elseif ( + $resource->getExtension() === 'svg' && + $targetFileExtension === 'svg' + ) { + // TYPO3 converts SVGs to PNGs if targetFileExtension is not set + $targetFileExtension = null; + } + if (!$skipProcessing) { $processingInstructions = [ 'crop' => $cropArea->makeAbsoluteBasedOnFile($resource), - 'fileExtension' => $queryParams['fileExtension'] ?? null, + 'fileExtension' => $targetFileExtension ?? null, 'height' => $queryParams['height'] ?? null, 'maxHeight' => $queryParams['maxHeight'] ?? null, 'maxWidth' => $queryParams['maxWidth'] ?? null, diff --git a/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript b/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript index 1f9ac84..85a8202 100644 --- a/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript +++ b/Configuration/TypoScript/Helpers/AssetProcessingConfiguration.typoscript @@ -2,6 +2,7 @@ lib.assetProcessingConfiguration { returnFlattenObject = 1 legacyReturn = 0 linkResult = 1 + delayProcessing = 1 properties { byType = 1 defaultFieldsByType = uidLocal,fileReferenceUid,type,extension,title,description,tx_headless_lazy_loading as lazyLoading