From a7e612a5b77ceaaf109b50043dce800dbf5275a4 Mon Sep 17 00:00:00 2001
From: Friedemann Altrock <hallo@faltrock.de>
Date: Thu, 31 Aug 2023 16:16:21 +0200
Subject: [PATCH] [BUGFIX] Respect crop of FileReference objects in
 getImgResource
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

ContentObjectRenderer::getImgResource is used for the content objects
IMAGE, IMG_RESOURCE, as well as GIFBUILDER to process images.

When it is passed a FileReference object, it should respect the crop
settings of this reference, same as when it is passed a reference UID
plus the treatIdAsReference config.

Resolves: #101810
Releases: main, 12.4, 11.5
Change-Id: I399106399ec283561c00fa5cf61d6c7fe2871e49
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80906
Tested-by: Stefan Bürk <stefan@buerk.tech>
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Stefan Bürk <stefan@buerk.tech>
---
 .../ContentObject/ContentObjectRenderer.php   |  4 +++-
 .../ContentObjectRendererTest.php             | 23 +++++++++++++++++++
 .../ContentObject/DataSet/FileReferences.csv  |  3 +++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index 55fb2b9da7b0..c194080ef2a6 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -3721,6 +3721,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
     public function getImgResource($file, $fileArray)
     {
         $importedFile = null;
+        $fileReference = null;
         if (empty($file) && empty($fileArray)) {
             return null;
         }
@@ -3742,6 +3743,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
             if ($file instanceof File) {
                 $fileObject = $file;
             } elseif ($file instanceof FileReference) {
+                $fileReference = $file;
                 $fileObject = $file->getOriginalFile();
             } else {
                 try {
@@ -3788,7 +3790,7 @@ class ContentObjectRenderer implements LoggerAwareInterface
                 $processingConfiguration['noScale'] = $this->stdWrapValue('noScale', $fileArray ?? []);
                 $processingConfiguration['additionalParameters'] = $this->stdWrapValue('params', $fileArray ?? []);
                 $processingConfiguration['frame'] = (int)$this->stdWrapValue('frame', $fileArray ?? []);
-                if (isset($fileReference)) {
+                if ($fileReference instanceof FileReference) {
                     $processingConfiguration['crop'] = $this->getCropAreaFromFileReference($fileReference, $fileArray);
                 } else {
                     $processingConfiguration['crop'] = $this->getCropAreaFromFromTypoScriptSettings($fileObject, $fileArray);
diff --git a/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
index 8fe05860e024..fae1ed990f0d 100644
--- a/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
+++ b/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php
@@ -1228,4 +1228,27 @@ And another one';
             self::assertSame($expectedParams, $jsonDecodedArray);
         }
     }
+
+    /**
+     * @test
+     */
+    public function getImgResourceRespectsFileReferenceObjectCropData(): void
+    {
+        $this->importCSVDataSet(__DIR__ . '/DataSet/FileReferences.csv');
+        $fileReferenceData = [
+            'uid' => 1,
+            'uid_local' => 1,
+            'crop' => '{"default":{"cropArea":{"x":0,"y":0,"width":0.5,"height":0.5},"selectedRatio":"NaN","focusArea":null}}',
+        ];
+        $fileReference = new FileReference($fileReferenceData);
+
+        $subject = GeneralUtility::makeInstance(ContentObjectRenderer::class);
+        $result = $subject->getImgResource($fileReference, []);
+
+        $expectedWidth = 512;
+        $expectedHeight = 341;
+
+        self::assertEquals($expectedWidth, $result[0]);
+        self::assertEquals($expectedHeight, $result[1]);
+    }
 }
diff --git a/typo3/sysext/frontend/Tests/Functional/ContentObject/DataSet/FileReferences.csv b/typo3/sysext/frontend/Tests/Functional/ContentObject/DataSet/FileReferences.csv
index 8cb29a6dbe87..3264363a3df7 100644
--- a/typo3/sysext/frontend/Tests/Functional/ContentObject/DataSet/FileReferences.csv
+++ b/typo3/sysext/frontend/Tests/Functional/ContentObject/DataSet/FileReferences.csv
@@ -8,3 +8,6 @@
 "sys_file_reference",,,,,,,,,,,,,,,,,,,,
 ,"uid","pid","title","uid_local","uid_foreign","sys_language_uid","l10n_parent","t3ver_wsid","t3ver_state","t3ver_stage","t3ver_oid","deleted","tablenames","fieldname","sorting_foreign","description","alternative","link","l10n_diffsource"
 ,1,1,"T3BOARD",1,297,0,0,0,0,0,0,0,"tt_content","image",2,,,,
+"sys_file_metadata",,,,,,,,,,,,,,,,,,,,,
+,uid,pid,sys_language_uid,l10n_parent,l10n_state,t3_origuid,l10n_diffsource,t3ver_oid,t3ver_wsid,t3ver_state,t3ver_stage,file,title,width,height,description,alternative,categories
+,1,0,0,0,,0,,0,0,0,0,1,,1024,683,,,0,
-- 
GitLab