From e94d79098287beea84c515adfd9e0d2ffd55281a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20L=C3=BCder?= Date: Tue, 18 Feb 2025 23:46:11 +1100 Subject: [PATCH 1/3] Added resizeAndCropImage to MiddlewareActionAbstract --- .../Middleware/MiddlewareActionAbstract.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Classes/Middleware/MiddlewareActionAbstract.php b/Classes/Middleware/MiddlewareActionAbstract.php index 7ca56f5..d6c9b76 100644 --- a/Classes/Middleware/MiddlewareActionAbstract.php +++ b/Classes/Middleware/MiddlewareActionAbstract.php @@ -9,6 +9,7 @@ use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Context\LanguageAspect; use TYPO3\CMS\Core\Context\UserAspect; +use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; use TYPO3\CMS\Core\Localization\LanguageService; use TYPO3\CMS\Core\Localization\LanguageServiceFactory; use TYPO3\CMS\Core\Routing\PageArguments; @@ -17,6 +18,7 @@ use TYPO3\CMS\Core\TypoScript\TemplateService; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Domain\Model\FileReference; +use TYPO3\CMS\Extbase\Service\ImageService; use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder; use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; @@ -147,4 +149,49 @@ protected function getLocalizationFromKey(string $keyToTranslate, string $langua return $keyToTranslate; } + + public function resizeAndCropImage(?FileReference $image, string $width = '2480', string $height = '770', string $cropArea = 'default', bool $absUrl = false): ?string { + if (null == $image) { + return null; + } + + if ('svg' == strtolower($image->getOriginalResource()->getExtension())) { + if ($absUrl) { + if (null == $this->siteLanguage) { + return '/'.$image->getOriginalResource()->getPublicUrl(); + } + + return $this->siteLanguage->getBase()->getScheme().'://'.$this->siteLanguage->getBase()->getHost().'/'.$image->getOriginalResource()->getPublicUrl(); + } + + return '/'.$image->getOriginalResource()->getPublicUrl(); + } + + $imageService = GeneralUtility::makeInstance(ImageService::class); + $getImage = $imageService->getImage('', $image, false); + + $cropString = null; + if ($getImage->hasProperty('crop') && $getImage->getProperty('crop')) { + $cropString = $getImage->getProperty('crop'); + } + + $cropVariantCollection = CropVariantCollection::create(strval($cropString)); + $cropArea = $cropVariantCollection->getCropArea($cropArea); + + $processedImage = $imageService->applyProcessingInstructions($getImage, [ + 'width' => $width, + 'height' => $height, + 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($getImage), + ]); + + if ($absUrl) { + if (null == $this->siteLanguage) { + return '/'.$processedImage->getPublicUrl(); + } + + return $this->siteLanguage->getBase()->getScheme().'://'.$this->siteLanguage->getBase()->getHost().'/'.$processedImage->getPublicUrl(); + } + + return '/'.$processedImage->getPublicUrl(); + } } From 8de889fc3919e98346b21a5b2f8ec5020f80c64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20L=C3=BCder?= Date: Wed, 19 Feb 2025 00:01:07 +1100 Subject: [PATCH 2/3] Fixed PHP CS --- Classes/Utility/FlexFormUtility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Utility/FlexFormUtility.php b/Classes/Utility/FlexFormUtility.php index 890c4e2..7450b68 100644 --- a/Classes/Utility/FlexFormUtility.php +++ b/Classes/Utility/FlexFormUtility.php @@ -67,7 +67,7 @@ public static function getFlexFormValue(array $flexForm, string $fieldName, stri } if (is_array($sheetArray)) { - return self::getFlexFormValueFromSheetArray($sheetArray, \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode('/', $fieldName), $value); + return self::getFlexFormValueFromSheetArray($sheetArray, GeneralUtility::trimExplode('/', $fieldName), $value); } return null; From cd2c58a1b9affa446f88add57fe4d41b1584b875 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20L=C3=BCder?= Date: Wed, 19 Feb 2025 00:01:25 +1100 Subject: [PATCH 3/3] Added array check to getFlexFormValueFromSheetArray --- Classes/Utility/FlexFormUtility.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Classes/Utility/FlexFormUtility.php b/Classes/Utility/FlexFormUtility.php index 7450b68..5f4bfb8 100644 --- a/Classes/Utility/FlexFormUtility.php +++ b/Classes/Utility/FlexFormUtility.php @@ -93,7 +93,11 @@ public static function getFlexFormValueFromSheetArray(array $sheetArray, array $ } } } else { - $tempArr = (array) ($tempArr[strval($v)] ?? []); + if (is_array($tempArr)) { + $tempArr = (array) ($tempArr[strval($v)] ?? []); + } else { + $tempArr = []; + } } }