From bfa2524cc5c69eae4bea6a08175cbfb2a321d4db Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9my=20DANIEL?= <dogawaf@no-log.org>
Date: Fri, 3 Mar 2023 19:21:12 +0100
Subject: [PATCH] [BUGFIX] Ensure scaled image size width/height are integers

Given the input width/heigth, getImageScale can return either int or float, depending of the calculated sizes.

This can triggers strict type warnings in calling code,
if width/heigth are expected to be integers.

In order to fix this, returned width/height are now always integers.

Resolves: #100076
Releases: main, 11.5
Change-Id: Ic29ba67f80a3a11a00a5b66f2644f923fa765064
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78019
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Sybille Peters <sypets@gmx.de>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
---
 .../core/Classes/Imaging/GraphicalFunctions.php  | 16 ++++++++--------
 .../Unit/Imaging/GraphicalFunctionsTest.php      |  8 ++++----
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php b/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
index 19181cd03cf9..7f83fbfc78d7 100644
--- a/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
+++ b/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
@@ -2377,28 +2377,28 @@ class GraphicalFunctions
         // If scaling should be performed. Check that input "info" array will not cause division-by-zero
         if (($w || $h) && $info[0] && $info[1]) {
             if ($w && !$h) {
-                $info[1] = ceil($info[1] * ($w / $info[0]));
+                $info[1] = (int)ceil($info[1] * ($w / $info[0]));
                 $info[0] = $w;
             }
             if (!$w && $h) {
-                $info[0] = ceil($info[0] * ($h / $info[1]));
+                $info[0] = (int)ceil($info[0] * ($h / $info[1]));
                 $info[1] = $h;
             }
             if ($w && $h) {
                 if ($max) {
                     $ratio = $info[0] / $info[1];
                     if ($h * $ratio > $w) {
-                        $h = round($w / $ratio);
+                        $h = (int)round($w / $ratio);
                     } else {
-                        $w = round($h * $ratio);
+                        $w = (int)round($h * $ratio);
                     }
                 }
                 if ($crs) {
                     $ratio = $info[0] / $info[1];
                     if ($h * $ratio < $w) {
-                        $h = round($w / $ratio);
+                        $h = (int)round($w / $ratio);
                     } else {
-                        $w = round($h * $ratio);
+                        $w = (int)round($h * $ratio);
                     }
                 }
                 $info[0] = $w;
@@ -2410,13 +2410,13 @@ class GraphicalFunctions
         // Set minimum-measures!
         if (isset($options['minW']) && $out[0] < $options['minW']) {
             if (($max || $crs) && $out[0]) {
-                $out[1] = round($out[1] * $options['minW'] / $out[0]);
+                $out[1] = (int)round($out[1] * $options['minW'] / $out[0]);
             }
             $out[0] = $options['minW'];
         }
         if (isset($options['minH']) && $out[1] < $options['minH']) {
             if (($max || $crs) && $out[1]) {
-                $out[0] = round($out[0] * $options['minH'] / $out[1]);
+                $out[0] = (int)round($out[0] * $options['minH'] / $out[1]);
             }
             $out[1] = $options['minH'];
         }
diff --git a/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php b/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
index 59863d73444f..efc84c0afeff 100644
--- a/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
+++ b/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
@@ -44,7 +44,7 @@ class GraphicalFunctionsTest extends UnitTestCase
                     'origH' => 0,
                     'max' => 0,
                     0 => 150,
-                    1 => (float)120,
+                    1 => 120,
                 ],
             ],
             'Get image scale with a maximum width of 100px' => [
@@ -63,7 +63,7 @@ class GraphicalFunctionsTest extends UnitTestCase
                     'origH' => 0,
                     'max' => 1,
                     0 => 100,
-                    1 => (float)80,
+                    1 => 80,
                 ],
             ],
             'Get image scale with a minimum width of 200px' => [
@@ -82,7 +82,7 @@ class GraphicalFunctionsTest extends UnitTestCase
                     'origH' => 0,
                     'max' => 0,
                     0 => 200,
-                    1 => (float)136,
+                    1 => 136,
                 ],
             ],
             'No PHP warning for zero in input dimensions when scaling' => [
@@ -109,7 +109,7 @@ class GraphicalFunctionsTest extends UnitTestCase
     public function getScaleForImage($info, $width, $height, $options, $expected): void
     {
         $result = (new GraphicalFunctions())->getImageScale($info, $width, $height, $options);
-        self::assertEquals($result, $expected);
+        self::assertSame($result, $expected);
     }
 
     /**
-- 
GitLab