From a470fda80f6a28cc40744aa0848900e7f1777baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Fromme?= Date: Sat, 22 Sep 2018 12:00:29 +0200 Subject: [PATCH] Proper treatment of cropping instructions --- .../Classes/Traits/ImageServiceTrait.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/web/typo3conf/ext/ep_products/Classes/Traits/ImageServiceTrait.php b/web/typo3conf/ext/ep_products/Classes/Traits/ImageServiceTrait.php index 16ff649b..968129f9 100644 --- a/web/typo3conf/ext/ep_products/Classes/Traits/ImageServiceTrait.php +++ b/web/typo3conf/ext/ep_products/Classes/Traits/ImageServiceTrait.php @@ -27,6 +27,8 @@ namespace EP\EpProducts\Traits; * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ +use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; +use TYPO3\CMS\Core\Resource\FileReference; use TYPO3\CMS\Core\Resource\FileRepository; use TYPO3\CMS\Extbase\Service\ImageService; @@ -63,20 +65,20 @@ trait ImageServiceTrait */ public function getImages($uid) { - if (!isset(self::$cache[$uid])) { + if (!isset(static::$cache[$uid])) { $original = $this->fileRepository->findByRelation( $this->table, $this->field, $uid ); $resized = $this->generateResizedImages($original); - self::$cache[$uid] = [ + static::$cache[$uid] = [ 'original' => $original, 'resized' => $resized, ]; } - return self::$cache[$uid]; + return static::$cache[$uid]; } /** @@ -90,7 +92,7 @@ trait ImageServiceTrait { foreach ($images as $image) { - $intructions['crop'] = $image->getProperty('crop'); + $instructions['crop'] = $this->getCroppingInfo($image, $instructions); $processedImage = $this->imageService->applyProcessingInstructions($image, $instructions); $resized[$version][] = [ 'url' => $this->imageService->getImageUri($processedImage, true), @@ -102,4 +104,17 @@ trait ImageServiceTrait return $resized; } + /** + * @param FileReference $image + * @param array $processingInstructions + * @return null|\TYPO3\CMS\Core\Imaging\ImageManipulation\Area + */ + protected function getCroppingInfo(FileReference $image, array $processingInstructions) + { + $cropString = $image->getProperty('crop'); + $cropVariantCollection = CropVariantCollection::create((string) $cropString); + $cropVariant = $processingInstructions['cropVariant'] ?: 'default'; + $cropArea = $cropVariantCollection->getCropArea($cropVariant); + return $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image); + } }