From d46f027dedb3b5f350d7635f82d95e6b85ddf372 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Thu, 21 Sep 2023 15:59:37 +0200
Subject: [PATCH] [BUGFIX] Avoid PHP 8 warning when converting crop in Image
 ViewHelpers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Resolves: #101185
Releases: main, 12.4, 11.5
Change-Id: If54f9308ac2928ce9521228593d6a3ef88713d51
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/81192
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Stefan Bürk <stefan@buerk.tech>
---
 .../sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php  | 8 +++++++-
 .../fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php     | 7 ++++++-
 .../Tests/Functional/ViewHelpers/ImageViewHelperTest.php  | 4 ++++
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php
index 3e952da407ad..a4155b37e236 100644
--- a/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php
+++ b/typo3/sysext/fluid/Classes/ViewHelpers/ImageViewHelper.php
@@ -120,7 +120,7 @@ class ImageViewHelper extends AbstractTagBasedViewHelper
         $this->registerArgument('src', 'string', 'a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead', false, '');
         $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
         $this->registerArgument('image', 'object', 'a FAL object (\\TYPO3\\CMS\\Core\\Resource\\File or \\TYPO3\\CMS\\Core\\Resource\\FileReference)');
-        $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
+        $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
         $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
         $this->registerArgument('fileExtension', 'string', 'Custom file extension to use');
 
@@ -158,6 +158,12 @@ class ImageViewHelper extends AbstractTagBasedViewHelper
             if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
                 $cropString = $image->getProperty('crop');
             }
+
+            // CropVariantCollection needs a string, but this VH could also receive an array
+            if (is_array($cropString)) {
+                $cropString = json_encode($cropString);
+            }
+
             $cropVariantCollection = CropVariantCollection::create((string)$cropString);
             $cropVariant = $this->arguments['cropVariant'] ?: 'default';
             $cropArea = $cropVariantCollection->getCropArea($cropVariant);
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php
index 0f047122a711..4710d24110de 100644
--- a/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php
+++ b/typo3/sysext/fluid/Classes/ViewHelpers/Uri/ImageViewHelper.php
@@ -94,7 +94,7 @@ class ImageViewHelper extends AbstractViewHelper
         $this->registerArgument('src', 'string', 'src', false, '');
         $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
         $this->registerArgument('image', 'object', 'image');
-        $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
+        $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
         $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
         $this->registerArgument('fileExtension', 'string', 'Custom file extension to use');
 
@@ -140,6 +140,11 @@ class ImageViewHelper extends AbstractViewHelper
                 $cropString = $image->getProperty('crop');
             }
 
+            // CropVariantCollection needs a string, but this VH could also receive an array
+            if (is_array($cropString)) {
+                $cropString = json_encode($cropString);
+            }
+
             $cropVariantCollection = CropVariantCollection::create((string)$cropString);
             $cropVariant = $arguments['cropVariant'] ?: 'default';
             $cropArea = $cropVariantCollection->getCropArea($cropVariant);
diff --git a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php
index 900de50a798b..6a4aa197ffb9 100644
--- a/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php
+++ b/typo3/sysext/fluid/Tests/Functional/ViewHelpers/ImageViewHelperTest.php
@@ -60,6 +60,10 @@ class ImageViewHelperTest extends FunctionalTestCase
                 '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="null" />',
                 '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@',
             ],
+            'crop as array' => [
+                '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="{\'x\': 200, \'y\': 200, \'width\': 200, \'height\': 200}" />',
+                '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@',
+            ],
             'jpg file extension' => [
                 '<f:image src="EXT:fluid/Tests/Functional/Fixtures/ViewHelpers/ImageViewHelperTest.jpg" width="300" height="500" crop="null" fileExtension="jpg" />',
                 '@^<img src="typo3temp/assets/_processed_/b/3/csm_ImageViewHelperTest_.*\.jpg" width="300" height="500" alt="" />$@',
-- 
GitLab