From f277d01bfb62e039a36b18b427c7222b46116e16 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/+/80928 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 55026bade2d3..86f8639625eb 100644 --- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php +++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php @@ -4039,6 +4039,7 @@ class ContentObjectRenderer implements LoggerAwareInterface public function getImgResource($file, $fileArray) { $importedFile = null; + $fileReference = null; if (empty($file) && empty($fileArray)) { return null; } @@ -4059,6 +4060,7 @@ class ContentObjectRenderer implements LoggerAwareInterface if ($file instanceof File) { $fileObject = $file; } elseif ($file instanceof FileReference) { + $fileReference = $file; $fileObject = $file->getOriginalFile(); } else { try { @@ -4105,7 +4107,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 24cfad0f5c6c..121bf132cdb6 100644 --- a/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Functional/ContentObject/ContentObjectRendererTest.php @@ -740,4 +740,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 d44afc82297d..a4c95ba00ff1 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","table_local","description","alternative","link","l10n_diffsource" ,1,1,"T3BOARD",1,297,0,0,0,0,0,0,0,"tt_content","image",2,"sys_file",,,, +"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