Skip to content

Commit

Permalink
[BUGFIX] Resolve issues with cropping handling
Browse files Browse the repository at this point in the history
Resolves: #740
  • Loading branch information
twoldanski committed Jul 23, 2024
1 parent d8da92c commit e997049
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
60 changes: 42 additions & 18 deletions Classes/DataProcessing/GalleryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -137,15 +154,20 @@ 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']);
foreach ($this->fileObjects as $key => $fileObject) {
$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,
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -419,7 +419,7 @@ private function cropVariant(ProcessingConfiguration $processingConfiguration, a
}

if (!$processingConfiguration->flattenProperties) {
$path .= 'dimensions/';
$path .= 'cropDimensions/';
}

$dimensions = [
Expand Down

0 comments on commit e997049

Please sign in to comment.