Skip to content

Commit

Permalink
[BUGFIX] Resolve issues with cropping handling (#754)
Browse files Browse the repository at this point in the history
Resolves: #740
  • Loading branch information
twoldanski committed Jul 24, 2024
1 parent f67c435 commit b3fb05a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
4 changes: 3 additions & 1 deletion Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
64 changes: 44 additions & 20 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] ?? [])
);
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;
}
Expand Down Expand Up @@ -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]
)
);
Expand Down
2 changes: 2 additions & 0 deletions Classes/Utility/File/ProcessingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 = [],
Expand Down
17 changes: 13 additions & 4 deletions Classes/Utility/FileUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -398,16 +400,17 @@ 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]);
}
}
}

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';
Expand All @@ -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) {
Expand Down

0 comments on commit b3fb05a

Please sign in to comment.