From e997049370514a3f45599a84c348e4fc329f2ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Wolda=C5=84ski?= Date: Tue, 23 Jul 2024 13:46:21 +0200 Subject: [PATCH] [BUGFIX] Resolve issues with cropping handling Resolves: #740 --- Classes/DataProcessing/GalleryProcessor.php | 60 ++++++++++++++------- Classes/Utility/FileUtility.php | 4 +- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/Classes/DataProcessing/GalleryProcessor.php b/Classes/DataProcessing/GalleryProcessor.php index fc3d07c8..7970de9e 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] ?? []) - ); - - $fileObj = $this->getFileUtility()->processCropVariants($image, $this->processorConfigurationObject, $fileObj); - } + 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 + ); $this->galleryData['rows'][$row]['columns'][$column] = $fileObj; } diff --git a/Classes/Utility/FileUtility.php b/Classes/Utility/FileUtility.php index 58868350..3dca20ad 100644 --- a/Classes/Utility/FileUtility.php +++ b/Classes/Utility/FileUtility.php @@ -398,7 +398,7 @@ public function processCropVariants( continue; } - $file = $this->process($originalFileReference, $processingConfiguration); + $file = $this->process($originalFileReference, $processingConfiguration->withOptions(['cropVariant' => $cropVariantName])); $processedFile['cropVariants'][$cropVariantName] = $this->cropVariant($processingConfiguration, $file); } } @@ -419,7 +419,7 @@ private function cropVariant(ProcessingConfiguration $processingConfiguration, a } if (!$processingConfiguration->flattenProperties) { - $path .= 'dimensions/'; + $path .= 'cropDimensions/'; } $dimensions = [