Skip to content

Commit

Permalink
[BUGFIX] Ensure scaled width and height are used with noScale=1
Browse files Browse the repository at this point in the history
If imgResource option `noScale = 1` [1] is configured,
the image itself is not scaled. However, the calculated
scaled width and height values needs to be used for the
`img` tag attributes `width` and `height`.

In that case, the orginal image is used instead of a
processed image, and the calculated values are discarded.

This change recalculates these values if `noScale = 1` is
requested to ensure that the scaled width and height values
are used correctly.

Example
-------

page.100 = IMAGE
page.100 {
  file = fileadmin/styleguide/bus_lane.jpg
  file.noScale = 1
  file.width = 240m
  file.height = 240m
  wrap = |<br>
}

[1] https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Functions/Imgresource.html#noscale

Resolves: #100972
Releases: main, 12.4
Change-Id: Ib09e3e837225347ee2e2a9c9979b014d8fd2b850
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81146
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
  • Loading branch information
sbuerk committed Sep 21, 2023
1 parent f08bbc9 commit 9e1735d
Showing 1 changed file with 23 additions and 0 deletions.
Expand Up @@ -16,6 +16,7 @@
namespace TYPO3\CMS\Core\Resource\Processing;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
use TYPO3\CMS\Core\Resource\FileInterface;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -203,6 +204,28 @@ public function processWithLocalFile(TaskInterface $task, string $originalFileNa
GeneralUtility::unlink_tempfile($croppedImage);
}

// If noScale option is applied, we need to reset the width and height to ensure the scaled values
// are used for the generated image tag even if the image itself is not scaled. This is needed, as
// the result is discarded due to the fact that the original image is used.
// @see https://forge.typo3.org/issues/100972
// Note: This should only happen if no image has been generated ($result === null).
if ($result === null && ($options['noScale'] ?? false)) {
$imageOperations = GeneralUtility::makeInstance(GraphicalFunctions::class);
$configuration = $task->getConfiguration();
$localProcessedFile = $task->getSourceFile()->getForLocalProcessing(false);
$imageDimensions = $imageOperations->getImageDimensions($localProcessedFile);
$imageScaleInfo = $imageOperations->getImageScale(
$imageDimensions,
$configuration['width'] ?? '',
$configuration['height'] ?? '',
$options
);
$targetFile->updateProperties([
'width' => $imageScaleInfo[0],
'height' => $imageScaleInfo[1],
]);
}

return $result;
}

Expand Down

0 comments on commit 9e1735d

Please sign in to comment.