[TASK] use horizontal ellipsis instead of 3 dots
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / MediaViewHelper.php
index d65d8d8..84f138c 100644 (file)
@@ -13,9 +13,12 @@ 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;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
 use TYPO3\CMS\Extbase\Service\ImageService;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
@@ -58,8 +61,6 @@ class MediaViewHelper extends AbstractTagBasedViewHelper
 
     /**
      * Initialize arguments.
-     *
-     * @return void
      */
     public function initializeArguments()
     {
@@ -70,6 +71,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');
     }
 
     /**
@@ -100,10 +102,9 @@ class MediaViewHelper extends AbstractTagBasedViewHelper
         // Fallback to image when no renderer is found
         if ($fileRenderer === null) {
             return $this->renderImage($file, $width, $height);
-        } else {
-            $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
-            return $fileRenderer->render($file, $width, $height, $additionalConfig);
         }
+        $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
+        return $fileRenderer->render($file, $width, $height, $additionalConfig);
     }
 
     /**
@@ -116,16 +117,25 @@ 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);
+        $cropArea = $cropVariantCollection->getCropArea($cropVariant);
         $processingInstructions = [
             'width' => $width,
             'height' => $height,
-            'crop' => $crop,
+            'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
         ];
         $imageService = $this->getImageService();
         $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
         $imageUri = $imageService->getImageUri($processedImage);
 
+        if (!$this->tag->hasAttribute('data-focus-area')) {
+            $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
+            if (!$focusArea->isEmpty()) {
+                $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
+            }
+        }
         $this->tag->addAttribute('src', $imageUri);
         $this->tag->addAttribute('width', $processedImage->getProperty('width'));
         $this->tag->addAttribute('height', $processedImage->getProperty('height'));
@@ -151,6 +161,6 @@ class MediaViewHelper extends AbstractTagBasedViewHelper
      */
     protected function getImageService()
     {
-        return $this->objectManager->get(ImageService::class);
+        return GeneralUtility::makeInstance(ImageService::class);
     }
 }