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