[BUGFIX] Add cropVariant support for broken GalleryProcessor 96/51596/3
authorFrank Naegler <frank.naegler@typo3.org>
Wed, 8 Feb 2017 20:24:35 +0000 (21:24 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Thu, 9 Feb 2017 02:52:36 +0000 (03:52 +0100)
Since the ImageManipulation wizard supports multiple crop variants, the
GalleryProcessor is broken. This patch add support for multiple crop variants
in the GalleryProcessor and MediaViewHelper

Resolves: #79698
Related: #75880
Releases: master
Change-Id: I588c5a4b3d44110fa8460094bd4c9fe950b5cc53
Reviewed-on: https://review.typo3.org/51596
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/fluid/Classes/ViewHelpers/MediaViewHelper.php
typo3/sysext/frontend/Classes/DataProcessing/GalleryProcessor.php

index d65d8d8..45c7027 100644 (file)
@@ -13,6 +13,8 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  *
  * The TYPO3 project - inspiring people to share!
  */
+
+use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Core\Resource\FileReference;
 use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry;
@@ -70,6 +72,7 @@ class MediaViewHelper extends AbstractTagBasedViewHelper
         $this->registerArgument('additionalConfig', 'string', 'This array can hold additional configuration that is passed though to the Renderer object', false, []);
         $this->registerArgument('width', 'string', 'This can be a numeric value representing the fixed width of in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
         $this->registerArgument('height', 'string', 'This can be a numeric value representing the fixed height in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
+        $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
     }
 
     /**
@@ -116,11 +119,13 @@ class MediaViewHelper extends AbstractTagBasedViewHelper
      */
     protected function renderImage(FileInterface $image, $width, $height)
     {
-        $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
+        $cropVariant = $this->arguments['cropVariant'] ?: 'default';
+        $cropString = $image instanceof FileReference ? $image->getProperty('crop') : '';
+        $cropVariantCollection = CropVariantCollection::create((string)$cropString);
         $processingInstructions = [
             'width' => $width,
             'height' => $height,
-            'crop' => $crop,
+            'crop' => $cropVariantCollection->getCropArea($cropVariant)->makeAbsoluteBasedOnFile($image),
         ];
         $imageService = $this->getImageService();
         $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
index 5edae4d..24a9988 100644 (file)
@@ -14,6 +14,7 @@ namespace TYPO3\CMS\Frontend\DataProcessing;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
 use TYPO3\CMS\Core\Resource\FileInterface;
 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
@@ -203,6 +204,11 @@ class GalleryProcessor implements DataProcessorInterface
     protected $borderPadding;
 
     /**
+     * @var string
+     */
+    protected $cropVariant = 'default';
+
+    /**
      * The (filtered) media files to be used in the gallery
      *
      * @var FileInterface[]
@@ -261,6 +267,7 @@ class GalleryProcessor implements DataProcessorInterface
         $this->borderEnabled = (bool)$this->getConfigurationValue('borderEnabled', 'imageborder');
         $this->borderWidth = (int)$this->getConfigurationValue('borderWidth');
         $this->borderPadding = (int)$this->getConfigurationValue('borderPadding');
+        $this->cropVariant = (int)$this->getConfigurationValue('cropVariant') ?: 'default';
 
         $this->determineGalleryPosition();
         $this->determineMaximumGalleryWidth();
@@ -424,7 +431,7 @@ class GalleryProcessor implements DataProcessorInterface
             // Recalculate gallery width
             $this->galleryData['width'] = floor($maximumRowWidth / $mediaScalingCorrection);
 
-        // User entered a predefined width
+            // User entered a predefined width
         } elseif ($this->equalMediaWidth) {
             $mediaScalingCorrection = 1;
 
@@ -448,10 +455,9 @@ class GalleryProcessor implements DataProcessorInterface
             // Recalculate gallery width
             $this->galleryData['width'] = floor($totalRowWidth / $mediaScalingCorrection);
 
-        // Automatic setting of width and height
+            // Automatic setting of width and height
         } else {
             $maxMediaWidth = (int)($galleryWidthMinusBorderAndSpacing / $this->galleryData['count']['columns']);
-
             foreach ($this->fileObjects as $key => $fileObject) {
                 $mediaWidth = min($maxMediaWidth, $this->getCroppedDimensionalProperty($fileObject, 'width'));
                 $mediaHeight = floor(
@@ -471,6 +477,7 @@ class GalleryProcessor implements DataProcessorInterface
      *
      * @param FileInterface $fileObject
      * @param string $dimensionalProperty 'width' or 'height'
+     *
      * @return int
      */
     protected function getCroppedDimensionalProperty(FileInterface $fileObject, $dimensionalProperty)
@@ -478,8 +485,10 @@ class GalleryProcessor implements DataProcessorInterface
         if (!$fileObject->hasProperty('crop') || empty($fileObject->getProperty('crop'))) {
             return $fileObject->getProperty($dimensionalProperty);
         }
-        $croppingConfiguration = json_decode($fileObject->getProperty('crop'), true);
-        return (int)$croppingConfiguration[$dimensionalProperty];
+
+        $croppingConfiguration = $fileObject->getProperty('crop');
+        $cropVariantCollection = CropVariantCollection::create((string)$croppingConfiguration);
+        return (int) $cropVariantCollection->getCropArea($this->cropVariant)->makeAbsoluteBasedOnFile($fileObject)->asArray()[$dimensionalProperty];
     }
 
     /**