‪TYPO3CMS  ‪main
MediaViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
26 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
27 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
28 
71 final class ‪MediaViewHelper extends AbstractTagBasedViewHelper
72 {
76  protected ‪$tagName = 'img';
77 
78  public function ‪initializeArguments(): void
79  {
80  parent::initializeArguments();
81  $this->registerUniversalTagAttributes();
82  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
83  $this->registerArgument('file', 'object', 'File', true);
84  $this->registerArgument('additionalConfig', 'array', 'This array can hold additional configuration that is passed though to the Renderer object', false, []);
85  $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.');
86  $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.');
87  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
88  $this->registerArgument('fileExtension', 'string', 'Custom file extension to use for images');
89  $this->registerArgument('loading', 'string', 'Native lazy-loading for images property. Can be "lazy", "eager" or "auto". Used on image files only.');
90  $this->registerArgument('decoding', 'string', 'Provides an image decoding hint to the browser. Can be "sync", "async" or "auto"', false);
91  }
92 
99  public function ‪render(): string
100  {
101  $file = $this->arguments['file'] ?? null;
102  $additionalConfig = (array)($this->arguments['additionalConfig'] ?? []);
103  $width = ($this->arguments['width'] ?? 0);
104  $height = ($this->arguments['height'] ?? 0);
105 
106  // get Resource Object (non ExtBase version)
107  if (is_callable([$file, 'getOriginalResource'])) {
108  // We have a domain model, so we need to fetch the FAL resource object from there
109  $file = $file->getOriginalResource();
110  }
111 
112  if (!$file instanceof FileInterface) {
113  throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface.', 1454252193);
114  }
115 
116  if ((string)($this->arguments['fileExtension'] ?? '') && !‪GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], (string)$this->arguments['fileExtension'])) {
117  throw new Exception(
118  'The extension ' . $this->arguments['fileExtension'] . ' is not specified in $GLOBALS[\'TYPO3_CONF_VARS\'][\'GFX\'][\'imagefile_ext\']'
119  . ' as a valid image file extension and can not be processed.',
120  1619030957
121  );
122  }
123 
124  $fileRenderer = GeneralUtility::makeInstance(RendererRegistry::class)->getRenderer($file);
125 
126  // Fallback to image when no renderer is found
127  if ($fileRenderer === null) {
128  return $this->‪renderImage($file, $width, $height, $this->arguments['fileExtension'] ?? null);
129  }
130  $arguments = [];
131  foreach ($this->arguments as $argumentName => $argumentValue) {
132  // Prevent "null" when given in fluid
133  if (!empty($argumentValue) && $argumentValue !== 'null') {
134  $arguments[$argumentName] = $argumentValue;
135  }
136  }
137  $additionalConfig = array_merge_recursive($arguments, $additionalConfig);
138  return $fileRenderer->render($file, $width, $height, $additionalConfig);
139  }
140 
148  protected function ‪renderImage(FileInterface $image, $width, $height, ?string $fileExtension): string
149  {
150  $cropVariant = (string)(($this->arguments['cropVariant'] ?? '') ?: 'default');
151  $cropString = $image instanceof FileReference ? $image->getProperty('crop') : '';
152  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
153  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
154  $processingInstructions = [
155  'width' => $width,
156  'height' => $height,
157  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
158  ];
159  if (!empty($fileExtension)) {
160  $processingInstructions['fileExtension'] = $fileExtension;
161  }
162  $imageService = $this->‪getImageService();
163  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
164  $imageUri = $imageService->getImageUri($processedImage);
165 
166  if (!$this->tag->hasAttribute('data-focus-area')) {
167  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
168  if (!$focusArea->isEmpty()) {
169  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
170  }
171  }
172  $this->tag->addAttribute('src', $imageUri);
173  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
174  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
175  if (in_array($this->arguments['loading'] ?? '', ['lazy', 'eager', 'auto'], true)) {
176  $this->tag->addAttribute('loading', $this->arguments['loading']);
177  }
178  if (in_array($this->arguments['decoding'] ?? '', ['sync', 'async', 'auto'], true)) {
179  $this->tag->addAttribute('decoding', $this->arguments['decoding']);
180  }
181 
182  $alt = $image->getProperty('alternative');
183  $title = $image->getProperty('title');
184 
185  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
186  if (empty($this->arguments['alt'])) {
187  $this->tag->addAttribute('alt', $alt);
188  }
189  if (empty($this->arguments['title']) && !empty($title)) {
190  $this->tag->addAttribute('title', $title);
191  }
192 
193  return $this->tag->render();
194  }
195 
196  protected function ‪getImageService(): ‪ImageService
197  {
198  return GeneralUtility::makeInstance(ImageService::class);
199  }
200 }
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\initializeArguments
‪initializeArguments()
Definition: MediaViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\renderImage
‪string renderImage(FileInterface $image, $width, $height, ?string $fileExtension)
Definition: MediaViewHelper.php:147
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\$tagName
‪string $tagName
Definition: MediaViewHelper.php:75
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:38
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper
Definition: MediaViewHelper.php:72
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\render
‪render()
Definition: MediaViewHelper.php:98
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\getImageService
‪getImageService()
Definition: MediaViewHelper.php:195
‪TYPO3\CMS\Fluid\ViewHelpers
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility\inList
‪static bool inList($list, $item)
Definition: GeneralUtility.php:422
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:26