From 7d58c5ca406d77ccd206aef9fe55340251e996d0 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/+/78057
Tested-by: core-ci <typo3@b13.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-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 6dce44fb91fe..d389594efed4 100644
--- a/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
+++ b/typo3/sysext/core/Classes/Imaging/GraphicalFunctions.php
@@ -2378,28 +2378,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;
@@ -2411,13 +2411,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 2aa7d749aa4e..4e9ffcf06439 100644
--- a/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
+++ b/typo3/sysext/core/Tests/Unit/Imaging/GraphicalFunctionsTest.php
@@ -46,7 +46,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' => [
@@ -65,7 +65,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' => [
@@ -84,7 +84,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' => [
@@ -111,7 +111,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