From f45b0c60957752637f2aced7777d3b9e07553c5d Mon Sep 17 00:00:00 2001 From: Sebastian Michaelsen Date: Fri, 1 Aug 2025 16:20:42 +0200 Subject: [PATCH] !chore: move image trait to helper --- Classes/DataProviding/Helpers/ImageHelper.php | 57 +++++++++++++++++++ Classes/DataProviding/Traits/Image.php | 47 --------------- 2 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 Classes/DataProviding/Helpers/ImageHelper.php delete mode 100644 Classes/DataProviding/Traits/Image.php diff --git a/Classes/DataProviding/Helpers/ImageHelper.php b/Classes/DataProviding/Helpers/ImageHelper.php new file mode 100644 index 0000000..4024746 --- /dev/null +++ b/Classes/DataProviding/Helpers/ImageHelper.php @@ -0,0 +1,57 @@ +getOriginalResource(); + } + if (!$image instanceof FileReference) { + return ''; + } + + $processingInstructions = [ + 'width' => $width, + 'height' => $height, + ]; + + $croppingArea = $this->getCroppingArea($image, $cropVariant); + if ($croppingArea !== null) { + $processingInstructions['crop'] = $croppingArea; + } + + $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions); + return $this->imageService->getImageUri($processedImage, $absolute); + } + + private function getCroppingArea(FileReference $image, string $cropVariant): ?Area + { + if (!$image->hasProperty('crop') || empty($image->getProperty('crop'))) { + return null; + } + + /** @var string $cropValue */ + $cropValue = $image->getProperty('crop'); + $cropVariantCollection = CropVariantCollection::create($cropValue); + $cropArea = $cropVariantCollection->getCropArea($cropVariant); + if ($cropArea->isEmpty()) { + return null; + } + return $cropArea->makeAbsoluteBasedOnFile($image); + } +} diff --git a/Classes/DataProviding/Traits/Image.php b/Classes/DataProviding/Traits/Image.php deleted file mode 100644 index 0f816f0..0000000 --- a/Classes/DataProviding/Traits/Image.php +++ /dev/null @@ -1,47 +0,0 @@ -imageService = $imageService; - } - - public function getImageUri($image, $width, $height, string $cropVariant = 'default', bool $absolute = false): string - { - if ($image instanceof ExtbaseFileReference) { - $image = $image->getOriginalResource(); - } - if (!$image instanceof FileReference) { - return ''; - } - if ($image->hasProperty('crop') && $image->getProperty('crop')) { - $cropString = $image->getProperty('crop'); - } - - $cropVariantCollection = CropVariantCollection::create((string)$cropString); - $cropArea = $cropVariantCollection->getCropArea($cropVariant); - $processingInstructions = [ - 'width' => $width, - 'height' => $height, - 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image), - ]; - if (!empty($arguments['fileExtension'])) { - $processingInstructions['fileExtension'] = $arguments['fileExtension']; - } - - $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions); - return $this->imageService->getImageUri($processedImage, $absolute); - } -}