From 9ef6c815c8dd691566ed7fb90ebd16182bb0cde1 Mon Sep 17 00:00:00 2001
From: Frans Saris <franssaris@gmail.com>
Date: Sun, 19 Feb 2017 11:46:03 +0100
Subject: [PATCH] [FEATURE] Add cropVariant support to typoscript rendering of
 images

The introduction of the new crop variants #75880 broke the handling of
cropped images when using typoscript to render file(reference)'s

This patch fixes the rendering of cropped images and introduces the new
cropVariant option to the typoscript object IMG_RESOURCE

Releated: #75880
Resolves: #79883
Releases: master
Change-Id: Icba77d76c6914d48dcc57d7e8c48471ed27c44b3
Reviewed-on: https://review.typo3.org/51753
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mona Muzaffar <mona.muzaffar@gmx.de>
Reviewed-by: Johannes Goslar <jogo@kronberger-spiele.de>
Reviewed-by: Christer V <cvi@systime.dk>
Tested-by: Christer V <cvi@systime.dk>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
---
 ...ntSupportToTyposcriptRenderingOfImages.rst | 34 +++++++++++++++++++
 .../ContentObject/ContentObjectRenderer.php   | 19 +++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Feature-79883-AddCropVariantSupportToTyposcriptRenderingOfImages.rst

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-79883-AddCropVariantSupportToTyposcriptRenderingOfImages.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-79883-AddCropVariantSupportToTyposcriptRenderingOfImages.rst
new file mode 100644
index 000000000000..b2d0f2dcdd02
--- /dev/null
+++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-79883-AddCropVariantSupportToTyposcriptRenderingOfImages.rst
@@ -0,0 +1,34 @@
+.. include:: ../../Includes.txt
+
+===========================================================================
+Feature: #79883 - Add cropVariant support to typoscript rendering of images
+===========================================================================
+
+See :issue:`79883`
+
+Description
+===========
+
+The introduction of the new crop variants :issue:`75880` broke the handling of
+cropped images when using typoscript to render file(reference)'s. This feature
+fixes this and introduces a new TypoScript option to use a different cropVariant.
+
+To use a different `cropVariant` as `default` you can provide the `cropVariant`
+name now in your TypoScript configuration. If :ts:`cropVariant` isn't provided
+the `default` variant will be used.
+
+.. code-block:: typoscript
+
+	# Use specific cropVariant for the images
+	tt_content.image.20.1.file.cropVariant = mobile
+
+
+
+Impact
+======
+
+If multiple cropVariants are available (see :issue:`75880`) you can now configure
+which variant to use with the :ts:`cropVariant` option of :ts:`imgResource`.
+
+
+.. index:: FAL, Frontend, TypoScript
\ No newline at end of file
diff --git a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
index 9beda03eb6cc..cdaf656c7086 100644
--- a/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
+++ b/typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
@@ -26,6 +26,7 @@ use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
 use TYPO3\CMS\Core\Database\Query\Restriction\FrontendRestrictionContainer;
 use TYPO3\CMS\Core\FrontendEditing\FrontendEditingController;
 use TYPO3\CMS\Core\Html\HtmlParser;
+use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
 use TYPO3\CMS\Core\LinkHandling\LinkService;
 use TYPO3\CMS\Core\Log\LogManager;
 use TYPO3\CMS\Core\Mail\MailMessage;
@@ -5035,7 +5036,7 @@ class ContentObjectRenderer
             } elseif ($file instanceof FileReference) {
                 $fileObject = $file->getOriginalFile();
                 if (!isset($fileArray['crop'])) {
-                    $fileArray['crop'] = $file->getProperty('crop');
+                    $fileArray['crop'] = $this->getCropArea($file, $fileArray['cropVariant'] ?: 'default');
                 }
             } else {
                 try {
@@ -5052,7 +5053,7 @@ class ContentObjectRenderer
                             $fileReference = $this->getResourceFactory()->getFileReferenceObject($file);
                             $fileObject = $fileReference->getOriginalFile();
                             if (!isset($fileArray['crop'])) {
-                                $fileArray['crop'] = $fileReference->getProperty('crop');
+                                $fileArray['crop'] = $this->getCropArea($fileReference, $fileArray['cropVariant'] ?: 'default');
                             }
                         } else {
                             $fileObject = $this->getResourceFactory()->getFileObject($file);
@@ -5155,6 +5156,20 @@ class ContentObjectRenderer
         return $imageResource;
     }
 
+    /**
+     * @param FileReference $fileReference
+     * @param string $cropVariant
+     * @return null|\TYPO3\CMS\Core\Imaging\ImageManipulation\Area
+     */
+    protected function getCropArea(FileReference $fileReference, string $cropVariant)
+    {
+        $cropVariantCollection = CropVariantCollection::create(
+            (string)$fileReference->getProperty('crop')
+        );
+        $cropArea = $cropVariantCollection->getCropArea($cropVariant);
+        return $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($fileReference);
+    }
+
     /***********************************************
      *
      * Data retrieval etc.
-- 
GitLab