‪TYPO3CMS  10.4
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 
68 class ‪MediaViewHelper extends AbstractTagBasedViewHelper
69 {
73  protected ‪$tagName = 'img';
74 
78  public function ‪initializeArguments()
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  }
91 
98  public function ‪render()
99  {
100  $file = $this->arguments['file'];
101  $additionalConfig = (array)$this->arguments['additionalConfig'];
102  $width = $this->arguments['width'];
103  $height = $this->arguments['height'];
104 
105  // get Resource Object (non ExtBase version)
106  if (is_callable([$file, 'getOriginalResource'])) {
107  // We have a domain model, so we need to fetch the FAL resource object from there
108  $file = $file->getOriginalResource();
109  }
110 
111  if (!$file instanceof ‪FileInterface) {
112  throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface.', 1454252193);
113  }
114 
115  $fileRenderer = ‪RendererRegistry::getInstance()->‪getRenderer($file);
116 
117  // Fallback to image when no renderer is found
118  if ($fileRenderer === null) {
119  return $this->‪renderImage($file, $width, $height, $this->arguments['fileExtension'] ?? null);
120  }
121  $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
122  return $fileRenderer->render($file, $width, $height, $additionalConfig);
123  }
124 
134  protected function ‪renderImage(‪FileInterface $image, $width, $height, ?string $fileExtension)
135  {
136  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
137  $cropString = $image instanceof ‪FileReference ? $image->‪getProperty('crop') : '';
138  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
139  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
140  $processingInstructions = [
141  'width' => $width,
142  'height' => $height,
143  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
144  ];
145  if (!empty($fileExtension)) {
146  $processingInstructions['fileExtension'] = $fileExtension;
147  }
148  $imageService = $this->‪getImageService();
149  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
150  $imageUri = $imageService->getImageUri($processedImage);
151 
152  if (!$this->tag->hasAttribute('data-focus-area')) {
153  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
154  if (!$focusArea->isEmpty()) {
155  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
156  }
157  }
158  $this->tag->addAttribute('src', $imageUri);
159  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
160  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
161  if (in_array($this->arguments['loading'] ?? '', ['lazy', 'eager', 'auto'], true)) {
162  $this->tag->addAttribute('loading', $this->arguments['loading']);
163  }
164 
165  $alt = $image->‪getProperty('alternative');
166  $title = $image->‪getProperty('title');
167 
168  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
169  if (empty($this->arguments['alt'])) {
170  $this->tag->addAttribute('alt', $alt);
171  }
172  if (empty($this->arguments['title']) && $title) {
173  $this->tag->addAttribute('title', $title);
174  }
175 
176  return $this->tag->render();
177  }
178 
184  protected function ‪getImageService()
185  {
186  return GeneralUtility::makeInstance(ImageService::class);
187  }
188 }
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\initializeArguments
‪initializeArguments()
Definition: MediaViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\getImageService
‪ImageService getImageService()
Definition: MediaViewHelper.php:183
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\renderImage
‪string renderImage(FileInterface $image, $width, $height, ?string $fileExtension)
Definition: MediaViewHelper.php:133
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\$tagName
‪string $tagName
Definition: MediaViewHelper.php:72
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getRenderer
‪FileRendererInterface null getRenderer(FileInterface $file)
Definition: RendererRegistry.php:125
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\render
‪string render()
Definition: MediaViewHelper.php:97
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper
Definition: MediaViewHelper.php:69
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getInstance
‪static RendererRegistry getInstance()
Definition: RendererRegistry.php:44
‪TYPO3\CMS\Fluid\ViewHelpers
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪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