Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow image processing for SVGs with crop variants #47

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Classes/Event/Listener/EnrichFileDataEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
68 changes: 42 additions & 26 deletions Classes/Middleware/AssetMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down