Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update image transforms #12046

Merged
merged 3 commits into from
Oct 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
- Fixed an error that could occur when passing an element query to a `relatedTo` param, if the parent element query contained any closures. ([#11981](https://github.com/craftcms/cms/issues/11981))
- Fixed a bug where unsaved drafts could be unintentionally deleted when saved, if a plugin or module was blocking the save via `EVENT_BEFORE_SAVE`. ([#12015](https://github.com/craftcms/cms/issues/12015))
- Fixed a bug where “Propagating tags” jobs would fail if two tags had similar titles.
- Fixed a bug where image transforms weren’t getting sized correctly in some cases when `upscaleImages` was disabled. ([#12023](https://github.com/craftcms/cms/issues/12023))

### Security
- Reduced the amount of system information that’s available to guest users.
12 changes: 6 additions & 6 deletions src/helpers/Image.php
Original file line number Diff line number Diff line change
@@ -80,12 +80,12 @@ public static function targetDimensions(
string $mode = 'crop',
?bool $upscale = null
): array {
if ($upscale ?? Craft::$app->getConfig()->getGeneral()->upscaleImages) {
[$width, $height] = static::calculateMissingDimension($transformWidth, $transformHeight, $sourceWidth, $sourceHeight);
[$width, $height] = static::calculateMissingDimension($transformWidth, $transformHeight, $sourceWidth, $sourceHeight);
$factor = max($sourceWidth / $width, $sourceHeight / $height);

if ($upscale ?? Craft::$app->getConfig()->getGeneral()->upscaleImages) {
// Special case for 'fit' since that's the only one whose dimensions vary from the transform dimensions
if ($mode === 'fit') {
$factor = max($sourceWidth / $width, $sourceHeight / $height);
$width = (int)round($sourceWidth / $factor);
$height = (int)round($sourceHeight / $factor);
}
@@ -102,9 +102,9 @@ public static function targetDimensions(
$imageRatio = $sourceWidth / $sourceHeight;

if ($mode === 'fit' || $imageRatio === $transformRatio) {
$targetWidth = min($sourceWidth, $transformWidth);
$targetHeight = min($sourceHeight, $transformHeight);
return static::calculateMissingDimension($targetWidth, $targetHeight, $sourceWidth, $sourceHeight);
$targetWidth = min($sourceWidth, $transformWidth, (int)round($sourceWidth / $factor));
$targetHeight = min($sourceHeight, $transformHeight, (int)round($sourceHeight / $factor));
return [$targetWidth, $targetHeight];
}

// Since we don't want to upscale, make sure the calculated ratios aren't bigger than the actual image size.
6 changes: 6 additions & 0 deletions tests/unit/helpers/ImageHelperTest.php
Original file line number Diff line number Diff line change
@@ -127,6 +127,12 @@ public function targetDimensionsDataProvider(): array
'fit8' => [67, 100, 400, 600, 200, 100, 'fit', true],
'fit9' => [67, 100, 40, 60, 200, 100, 'fit', true],
'fit10' => [40, 60, 40, 60, 200, 100, 'fit', false],

// https://github.com/craftcms/cms/issues/12023
'fit11' => [160, 240, 240, 360, 240, 240, 'fit', false],
'fit12' => [240, 160, 360, 240, 240, 240, 'fit', false],
'fit13' => [160, 240, 240, 360, 240, 240, 'fit', true],
'fit14' => [240, 160, 360, 240, 240, 240, 'fit', true],
];
}