Skip to content

Commit

Permalink
Don't stretch image beyond its original dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kitzberger committed Aug 29, 2023
1 parent 582464b commit 5c3853b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Classes/DataProcessing/FilesProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ protected function processFiles(array $properties = []): ?array
);

// 2. render additional formats
$originalWidth = $fileObject->getProperty('width');
$originalHeight = $fileObject->getProperty('height');
$targetWidth = (int)($properties['width'] ?? $file['properties']['dimensions']['width']);
$targetHeight = (int)($properties['height'] ?? $file['properties']['dimensions']['height']);
if ($targetWidth || $targetHeight) {
Expand All @@ -218,8 +220,10 @@ protected function processFiles(array $properties = []): ?array
$properties,
[
'fileExtension' => $formatConf['fileExtension'] ?? null,
'width' => $targetWidth * $factor,
'height' => $targetHeight * $factor,
// multiply width/height by factor,
// but don't stretch image beyond its original dimensions!
'width' => min($targetWidth * $factor, $originalWidth),
'height' => min($targetHeight * $factor, $originalHeight),
]
),
$cropVariant,
Expand Down
10 changes: 8 additions & 2 deletions Classes/DataProcessing/GalleryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ protected function prepareGalleryData()
);

// 2. render additional formats
$originalWidth = $image->getProperty('width');
$originalHeight = $image->getProperty('height');
$targetWidth = $fileObj['properties']['dimensions']['width'];
$targetHeight = $fileObj['properties']['dimensions']['height'];
foreach ($formats ?? [] as $formatKey => $formatConf) {
$formatKey = rtrim($formatKey, '.');
$factor = (float)($formatConf['factor'] ?? 1.0);
Expand All @@ -223,8 +227,10 @@ protected function prepareGalleryData()
$image,
[
'fileExtension' => $formatConf['fileExtension'] ?? null,
'width' => $fileObj['properties']['dimensions']['width'] * $factor,
'height' => $fileObj['properties']['dimensions']['height'] * $factor,
// multiply width/height by factor,
// but don't stretch image beyond its original dimensions!
'width' => min($targetWidth * $factor, $originalWidth),
'height' => min($targetHeight * $factor, $originalHeight),
]
)['publicUrl'];
}
Expand Down

0 comments on commit 5c3853b

Please sign in to comment.