From b3fb05a3b0bc2b0040ae538b00a9a7dd134ff619 Mon Sep 17 00:00:00 2001 From: twoldanski <66474451+twoldanski@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:02:49 +0200 Subject: [PATCH] [BUGFIX] Resolve issues with cropping handling (#754) Resolves: #740 --- Classes/DataProcessing/FilesProcessor.php | 4 +- Classes/DataProcessing/GalleryProcessor.php | 64 +++++++++++++------ .../Utility/File/ProcessingConfiguration.php | 2 + Classes/Utility/FileUtility.php | 17 +++-- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/Classes/DataProcessing/FilesProcessor.php b/Classes/DataProcessing/FilesProcessor.php index 1b69d447..33d3e6a3 100644 --- a/Classes/DataProcessing/FilesProcessor.php +++ b/Classes/DataProcessing/FilesProcessor.php @@ -184,7 +184,9 @@ protected function processFiles(array $properties = []): ?array $processingConfiguration, ); - $data[$key] = $this->getFileUtility()->processCropVariants($fileObject, $processingConfiguration, $data[$key]); + if (!$processingConfiguration->delayProcessing) { + $data[$key] = $this->getFileUtility()->processCropVariants($fileObject, $processingConfiguration, $data[$key]); + } } if ($processingConfiguration->flattenObject) { diff --git a/Classes/DataProcessing/GalleryProcessor.php b/Classes/DataProcessing/GalleryProcessor.php index fc3d07c8..afff5212 100644 --- a/Classes/DataProcessing/GalleryProcessor.php +++ b/Classes/DataProcessing/GalleryProcessor.php @@ -91,8 +91,14 @@ protected function calculateMediaWidthsAndHeights() if ($fileKey > $this->galleryData['count']['files'] - 1) { break 2; } - $currentMediaScaling = $this->equalMediaHeight / max($this->getCroppedDimensionalPropertyFromProcessedFile($this->fileObjects[$fileKey], 'height'), 1); - $totalRowWidth += $this->getCroppedDimensionalPropertyFromProcessedFile($this->fileObjects[$fileKey], 'width') * $currentMediaScaling; + $currentMediaScaling = $this->equalMediaHeight / max($this->getCroppedDimensionalPropertyFromProcessedFile( + $this->fileObjects[$fileKey], + 'height' + ), 1); + $totalRowWidth += $this->getCroppedDimensionalPropertyFromProcessedFile( + $this->fileObjects[$fileKey], + 'width' + ) * $currentMediaScaling; } $maximumRowWidth = max($totalRowWidth, $maximumRowWidth); $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing; @@ -103,7 +109,13 @@ protected function calculateMediaWidthsAndHeights() foreach ($this->fileObjects as $key => $fileObject) { $mediaHeight = floor($this->equalMediaHeight / $mediaScalingCorrection); $mediaWidth = floor( - $this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'width') * ($mediaHeight / max($this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'height'), 1)) + $this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'width' + ) * ($mediaHeight / max($this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'height' + ), 1)) ); $this->mediaDimensions[$key] = [ 'width' => $mediaWidth, @@ -113,7 +125,6 @@ protected function calculateMediaWidthsAndHeights() // Recalculate gallery width $this->galleryData['width'] = floor($maximumRowWidth / $mediaScalingCorrection); - // User entered a predefined width } elseif ($this->equalMediaWidth) { $mediaScalingCorrection = 1; @@ -127,7 +138,13 @@ protected function calculateMediaWidthsAndHeights() foreach ($this->fileObjects as $key => $fileObject) { $mediaWidth = floor($this->equalMediaWidth / $mediaScalingCorrection); $mediaHeight = floor( - $this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'width'), 1)) + $this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'height' + ) * ($mediaWidth / max($this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'width' + ), 1)) ); $this->mediaDimensions[$key] = [ 'width' => $mediaWidth, @@ -137,7 +154,6 @@ protected function calculateMediaWidthsAndHeights() // Recalculate gallery width $this->galleryData['width'] = floor($totalRowWidth / $mediaScalingCorrection); - // Automatic setting of width and height } else { $maxMediaWidth = (int)($galleryWidthMinusBorderAndSpacing / $this->galleryData['count']['columns']); @@ -145,7 +161,13 @@ protected function calculateMediaWidthsAndHeights() $croppedWidth = $this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'width'); $mediaWidth = $croppedWidth > 0 ? min($maxMediaWidth, $croppedWidth) : $maxMediaWidth; $mediaHeight = floor( - $this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalPropertyFromProcessedFile($fileObject, 'width'), 1)) + $this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'height' + ) * ($mediaWidth / max($this->getCroppedDimensionalPropertyFromProcessedFile( + $fileObject, + 'width' + ), 1)) ); $this->mediaDimensions[$key] = [ 'width' => $mediaWidth, @@ -197,17 +219,19 @@ protected function prepareGalleryData() $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1; $fileObj = $this->fileObjects[$fileKey] ?? null; - if ($fileObj) { - if ((($fileObj['properties']['type'] ?? '') === 'image' || ($fileObj['type'] ?? '') === 'image')) { - $src = $this->processorConfigurationObject->legacyReturn ? $fileObj['properties']['fileReferenceUid'] : $fileObj['fileReferenceUid']; - $image = $this->getImageService()->getImage((string)$src, null, true); - $fileObj = $this->getFileUtility()->process( - $image, - $this->processorConfigurationObject->withOptions($this->mediaDimensions[$fileKey] ?? []) - ); + if ($fileObj !== null && (($fileObj['properties']['type'] ?? '') === 'image' || ($fileObj['type'] ?? '') === 'image')) { + $src = $this->processorConfigurationObject->legacyReturn ? $fileObj['properties']['fileReferenceUid'] : $fileObj['fileReferenceUid']; + $image = $this->getImageService()->getImage((string)$src, null, true); + $fileObj = $this->getFileUtility()->process( + $image, + $this->processorConfigurationObject->withOptions($this->mediaDimensions[$fileKey] ?? []) + ); - $fileObj = $this->getFileUtility()->processCropVariants($image, $this->processorConfigurationObject, $fileObj); - } + $fileObj = $this->getFileUtility()->processCropVariants( + $image, + $this->processorConfigurationObject, + $fileObj + ); $this->galleryData['rows'][$row]['columns'][$column] = $fileObj; } @@ -244,13 +268,13 @@ protected function getImageService(): ImageService */ private function createFileObject(array $processedFile): FileInterface { - $uid = (int)$processedFile['properties']['uidLocal']; + $uid = $this->processorConfigurationObject->legacyReturn ? (int)$processedFile['properties']['uidLocal'] : $processedFile['uidLocal']; if (!isset($this->fileReferenceCache[$uid])) { $this->fileReferenceCache[$uid] = GeneralUtility::makeInstance( FileReference::class, array_merge( - $processedFile['properties'], - $processedFile['properties']['dimensions'], + $this->processorConfigurationObject->legacyReturn ? $processedFile['properties'] : $processedFile, + $this->processorConfigurationObject->legacyReturn ? $processedFile['properties']['dimensions'] : $processedFile['dimensions'], ['uid_local' => $uid] ) ); diff --git a/Classes/Utility/File/ProcessingConfiguration.php b/Classes/Utility/File/ProcessingConfiguration.php index 1102b167..53ea5b35 100644 --- a/Classes/Utility/File/ProcessingConfiguration.php +++ b/Classes/Utility/File/ProcessingConfiguration.php @@ -42,6 +42,7 @@ public static function fromOptions(array $options): static ((int)($options['properties.']['byType'] ?? 0)) > 0, (int)($options['processPdfAsImage'] ?? 0) > 0, (int)($options['processSvg'] ?? 0) > 0, + (int)($options['outputCropArea'] ?? 0) > 0, GeneralUtility::trimExplode(',', $options['properties.']['includeOnly'] ?? '', true), GeneralUtility::trimExplode(',', $options['properties.']['defaultFieldsByType'] ?? '', true), GeneralUtility::trimExplode(',', $options['properties.']['defaultImageFields'] ?? '', true), @@ -70,6 +71,7 @@ private function __construct( public readonly bool $propertiesByType = false, public readonly bool $processPdfAsImage = false, public readonly bool $processSvg = false, + public readonly bool $outputCropArea = false, public readonly array $includeProperties = [], public readonly array $defaultFieldsByType = [], public readonly array $defaultImageFields = [], diff --git a/Classes/Utility/FileUtility.php b/Classes/Utility/FileUtility.php index 58868350..c852af09 100644 --- a/Classes/Utility/FileUtility.php +++ b/Classes/Utility/FileUtility.php @@ -386,7 +386,9 @@ public function processCropVariants( $crop = $originalFileReference->getProperty('crop'); if ($crop !== null) { - unset($processedFile['crop'], $processedFile['properties']['crop']); + if (!$processingConfiguration->legacyReturn) { + unset($processedFile['crop'], $processedFile['properties']['crop']); + } $cropVariants = json_decode($originalFileReference->getProperty('crop'), true); @@ -398,8 +400,9 @@ public function processCropVariants( continue; } + $processingConfiguration = $processingConfiguration->withOptions(['cropVariant' => $cropVariantName]); $file = $this->process($originalFileReference, $processingConfiguration); - $processedFile['cropVariants'][$cropVariantName] = $this->cropVariant($processingConfiguration, $file); + $processedFile['cropVariants'][$cropVariantName] = $this->cropVariant($processingConfiguration, $file, $cropVariants[$cropVariantName]); } } } @@ -407,7 +410,7 @@ public function processCropVariants( return $processedFile; } - private function cropVariant(ProcessingConfiguration $processingConfiguration, array $file): array + private function cropVariant(ProcessingConfiguration $processingConfiguration, array $file, array $cropVariant = []): array { $url = $processingConfiguration->legacyReturn ? $file['publicUrl'] : $file['url']; $urlKey = $processingConfiguration->legacyReturn ? 'publicUrl' : 'url'; @@ -419,12 +422,18 @@ private function cropVariant(ProcessingConfiguration $processingConfiguration, a } if (!$processingConfiguration->flattenProperties) { - $path .= 'dimensions/'; + $path .= 'cropDimensions/'; + } + + $additional = []; + if ($processingConfiguration->outputCropArea) { + $additional = ['crop' => $cropVariant]; } $dimensions = [ 'width' => ArrayUtility::getValueByPath($file, $path . 'width'), 'height' => ArrayUtility::getValueByPath($file, $path . 'height'), + ...$additional, ]; if (!$processingConfiguration->legacyReturn && $processingConfiguration->flattenProperties) {