diff --git a/Classes/Event/FileDataAfterCropVariantProcessingEvent.php b/Classes/Event/FileDataAfterCropVariantProcessingEvent.php new file mode 100644 index 00000000..0265141b --- /dev/null +++ b/Classes/Event/FileDataAfterCropVariantProcessingEvent.php @@ -0,0 +1,51 @@ +processedFile = $processedFile; + } + + public function getProcessedFile(): array + { + return $this->processedFile; + } + + public function setProcessedFile(array $processedFile): void + { + $this->processedFile = $processedFile; + } + + public function getOriginal(): FileInterface + { + return $this->originalFileReference; + } + + public function getProcessingConfiguration(): ProcessingConfiguration + { + return $this->processingConfiguration; + } +} diff --git a/Classes/Utility/FileUtility.php b/Classes/Utility/FileUtility.php index c852af09..144a0321 100644 --- a/Classes/Utility/FileUtility.php +++ b/Classes/Utility/FileUtility.php @@ -12,8 +12,10 @@ namespace FriendsOfTYPO3\Headless\Utility; use FriendsOfTYPO3\Headless\Event\EnrichFileDataEvent; +use FriendsOfTYPO3\Headless\Event\FileDataAfterCropVariantProcessingEvent; use FriendsOfTYPO3\Headless\Utility\File\ProcessingConfiguration; use Psr\EventDispatcher\EventDispatcherInterface; +use Throwable; use TYPO3\CMS\Core\Configuration\Features; use TYPO3\CMS\Core\Http\NormalizedParams; use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; @@ -103,7 +105,11 @@ public function process(FileInterface $fileReference, ProcessingConfiguration $p $disableProcessingFor[] = 'image/svg+xml'; } - if (!$processingConfiguration->delayProcessing && !in_array($fileReference->getMimeType(), $disableProcessingFor, true)) { + if (!$processingConfiguration->delayProcessing && !in_array( + $fileReference->getMimeType(), + $disableProcessingFor, + true + )) { $fileReference = $this->processImageFile($fileReference, $processingConfiguration); } $publicUrl = $this->imageService->getImageUri($fileReference, true); @@ -188,7 +194,12 @@ public function process(FileInterface $fileReference, ProcessingConfiguration $p $processedFile = array_merge($processedFile, $processedProperties); if ($processingConfiguration->autogenerate !== []) { - $processedFile = $this->processAutogenerate($originalFileReference, $fileReference, $processedFile, $processingConfiguration); + $processedFile = $this->processAutogenerate( + $originalFileReference, + $fileReference, + $processedFile, + $processingConfiguration + ); } return $processedFile; @@ -232,9 +243,32 @@ private function onDemandProperties(ProcessingConfiguration $processingConfigura private function filterProperties(ProcessingConfiguration $processingConfiguration, array $properties): array { - $allowedDefault = $processingConfiguration->defaultFieldsByType !== [] ? $processingConfiguration->defaultFieldsByType : ['type', 'size', 'title', 'alternative', 'description', 'uidLocal', 'fileReferenceUid', 'mimeType']; - $allowedForImages = array_merge($allowedDefault, $processingConfiguration->defaultImageFields !== [] ? $processingConfiguration->defaultImageFields : ['dimensions', 'link', 'linkData']); - $allowedForVideo = array_merge($allowedDefault, $processingConfiguration->defaultVideoFields !== [] ? $processingConfiguration->defaultVideoFields : ['dimensions', 'autoplay', 'originalUrl']); + $allowedDefault = $processingConfiguration->defaultFieldsByType !== [] ? $processingConfiguration->defaultFieldsByType : [ + 'type', + 'size', + 'title', + 'alternative', + 'description', + 'uidLocal', + 'fileReferenceUid', + 'mimeType', + ]; + $allowedForImages = array_merge( + $allowedDefault, + $processingConfiguration->defaultImageFields !== [] ? $processingConfiguration->defaultImageFields : [ + 'dimensions', + 'link', + 'linkData', + ] + ); + $allowedForVideo = array_merge( + $allowedDefault, + $processingConfiguration->defaultVideoFields !== [] ? $processingConfiguration->defaultVideoFields : [ + 'dimensions', + 'autoplay', + 'originalUrl', + ] + ); $allowed = match ($properties['type']) { 'video' => $allowedForVideo, @@ -343,8 +377,12 @@ protected function translate(string $key, string $extensionName): ?string return LocalizationUtility::translate($key, $extensionName); } - private function processAutogenerate(FileInterface $originalReference, FileInterface $fileReference, array $processedFile, ProcessingConfiguration $processingConfiguration): array - { + private function processAutogenerate( + FileInterface $originalReference, + FileInterface $fileReference, + array $processedFile, + ProcessingConfiguration $processingConfiguration + ): array { $originalWidth = $originalReference->getProperty('width'); $originalHeight = $originalReference->getProperty('height'); $targetWidth = (int)($processingConfiguration->width !== '' ? $processingConfiguration->width : $fileReference->getProperty('width')); @@ -394,7 +432,10 @@ public function processCropVariants( $collection = CropVariantCollection::create($originalFileReference->getProperty('crop')); - if (is_array($cropVariants) && count($cropVariants) > 1 && str_starts_with($originalFileReference->getMimeType(), 'image/')) { + if (is_array($cropVariants) && count($cropVariants) > 1 && str_starts_with( + $originalFileReference->getMimeType(), + 'image/' + )) { foreach (array_keys($cropVariants) as $cropVariantName) { if ($processingConfiguration->conditionalCropVariant && $collection->getCropArea($cropVariantName)->isEmpty()) { continue; @@ -402,16 +443,29 @@ public function processCropVariants( $processingConfiguration = $processingConfiguration->withOptions(['cropVariant' => $cropVariantName]); $file = $this->process($originalFileReference, $processingConfiguration); - $processedFile['cropVariants'][$cropVariantName] = $this->cropVariant($processingConfiguration, $file, $cropVariants[$cropVariantName]); + $processedFile['cropVariants'][$cropVariantName] = $this->cropVariant( + $processingConfiguration, + $file, + $cropVariants[$cropVariantName] + ); } } } - return $processedFile; + return $this->eventDispatcher->dispatch( + new FileDataAfterCropVariantProcessingEvent( + $originalFileReference, + $processingConfiguration, + $processedFile + ) + )->getProcessedFile(); } - private function cropVariant(ProcessingConfiguration $processingConfiguration, array $file, array $cropVariant = []): array - { + private function cropVariant( + ProcessingConfiguration $processingConfiguration, + array $file, + array $cropVariant = [] + ): array { $url = $processingConfiguration->legacyReturn ? $file['publicUrl'] : $file['url']; $urlKey = $processingConfiguration->legacyReturn ? 'publicUrl' : 'url'; @@ -422,17 +476,26 @@ private function cropVariant(ProcessingConfiguration $processingConfiguration, a } if (!$processingConfiguration->flattenProperties) { - $path .= 'cropDimensions/'; + $path .= 'dimensions/'; } $additional = []; + if ($processingConfiguration->outputCropArea) { $additional = ['crop' => $cropVariant]; } + try { + $width = ArrayUtility::getValueByPath($file, $path . 'width'); + $height = ArrayUtility::getValueByPath($file, $path . 'height'); + } catch (Throwable) { + $width = 0; + $height = 0; + } + $dimensions = [ - 'width' => ArrayUtility::getValueByPath($file, $path . 'width'), - 'height' => ArrayUtility::getValueByPath($file, $path . 'height'), + 'width' => $width, + 'height' => $height, ...$additional, ];