‪TYPO3CMS  9.5
MediaViewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
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  }
89 
96  public function ‪render()
97  {
98  $file = $this->arguments['file'];
99  $additionalConfig = (array)$this->arguments['additionalConfig'];
100  $width = $this->arguments['width'];
101  $height = $this->arguments['height'];
102 
103  // get Resource Object (non ExtBase version)
104  if (is_callable([$file, 'getOriginalResource'])) {
105  // We have a domain model, so we need to fetch the FAL resource object from there
106  $file = $file->getOriginalResource();
107  }
108 
109  if (!($file instanceof ‪FileInterface || $file instanceof ‪AbstractFileFolder)) {
110  throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
111  }
112 
113  $fileRenderer = ‪RendererRegistry::getInstance()->‪getRenderer($file);
114 
115  // Fallback to image when no renderer is found
116  if ($fileRenderer === null) {
117  return $this->‪renderImage($file, $width, $height);
118  }
119  $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
120  return $fileRenderer->render($file, $width, $height, $additionalConfig);
121  }
122 
131  protected function ‪renderImage(‪FileInterface $image, $width, $height)
132  {
133  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
134  $cropString = $image instanceof ‪FileReference ? $image->‪getProperty('crop') : '';
135  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
136  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
137  $processingInstructions = [
138  'width' => $width,
139  'height' => $height,
140  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
141  ];
142  $imageService = $this->‪getImageService();
143  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
144  $imageUri = $imageService->getImageUri($processedImage);
145 
146  if (!$this->tag->hasAttribute('data-focus-area')) {
147  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
148  if (!$focusArea->isEmpty()) {
149  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
150  }
151  }
152  $this->tag->addAttribute('src', $imageUri);
153  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
154  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
155 
156  $alt = $image->‪getProperty('alternative');
157  $title = $image->‪getProperty('title');
158 
159  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
160  if (empty($this->arguments['alt'])) {
161  $this->tag->addAttribute('alt', $alt);
162  }
163  if (empty($this->arguments['title']) && $title) {
164  $this->tag->addAttribute('title', $title);
165  }
166 
167  return $this->tag->render();
168  }
169 
175  protected function ‪getImageService()
176  {
177  return GeneralUtility::makeInstance(ImageService::class);
178  }
179 }
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\initializeArguments
‪initializeArguments()
Definition: MediaViewHelper.php:77
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\getImageService
‪ImageService getImageService()
Definition: MediaViewHelper.php:174
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪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:123
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\render
‪string render()
Definition: MediaViewHelper.php:95
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper
Definition: MediaViewHelper.php:69
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Fluid\ViewHelpers\MediaViewHelper\renderImage
‪string renderImage(FileInterface $image, $width, $height)
Definition: MediaViewHelper.php:130
‪TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder
Definition: AbstractFileFolder.php:23
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry\getInstance
‪static RendererRegistry getInstance()
Definition: RendererRegistry.php:42
‪TYPO3\CMS\Fluid\ViewHelpers
Definition: BaseViewHelper.php:2
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:40
‪TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
Definition: RendererRegistry.php:24