From eba0ea7ecd4edaa73da823ecf4ce7f4f703cd23f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20B=C3=BCrk?= <stefan@buerk.tech>
Date: Thu, 21 Sep 2023 10:07:45 +0200
Subject: [PATCH] [BUGFIX] Ensure scaled width and height are used with
 `noScale=1`
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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/+/81130
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Tested-by: Benni Mack <benni@typo3.org>
---
 .../Processing/LocalCropScaleMaskHelper.php   | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/typo3/sysext/core/Classes/Resource/Processing/LocalCropScaleMaskHelper.php b/typo3/sysext/core/Classes/Resource/Processing/LocalCropScaleMaskHelper.php
index 2d2c4aeb825b..8fc2259d4992 100644
--- a/typo3/sysext/core/Classes/Resource/Processing/LocalCropScaleMaskHelper.php
+++ b/typo3/sysext/core/Classes/Resource/Processing/LocalCropScaleMaskHelper.php
@@ -158,6 +158,27 @@ class LocalCropScaleMaskHelper
             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)) {
+            $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;
     }
 
-- 
GitLab