2 namespace TYPO3\CMS\Fluid\ViewHelpers
;
5 * This file is part of the TYPO3 CMS project.
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.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
17 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
;
18 use TYPO3\CMS\Core\Resource\FileInterface
;
19 use TYPO3\CMS\Core\Resource\FileReference
;
20 use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry
;
21 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder
;
22 use TYPO3\CMS\Extbase\Service\ImageService
;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
;
26 * Render a given media file with the correct html tag.
28 * It asks the RendererRegister for the correct Renderer class and if not found it falls
29 * back to the ImageViewHelper as that is the "Renderer" class for images in Fluid context.
33 * <code title="Image Object">
34 * <f:media file="{file}" width="400" height="375" />
37 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
40 * <code title="MP4 Video Object">
41 * <f:media file="{file}" width="400" height="375" />
44 * <video width="400" height="375" controls><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
47 * <code title="MP4 Video Object with loop and autoplay option set">
48 * <f:media file="{file}" width="400" height="375" additionalConfig="{loop: '1', autoplay: '1'}" />
51 * <video width="400" height="375" controls loop><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
54 class MediaViewHelper
extends AbstractTagBasedViewHelper
59 protected $tagName = 'img';
62 * Initialize arguments.
66 public function initializeArguments()
68 parent
::initializeArguments();
69 $this->registerUniversalTagAttributes();
70 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false
);
71 $this->registerArgument('file', 'object', 'File', true
);
72 $this->registerArgument('additionalConfig', 'string', 'This array can hold additional configuration that is passed though to the Renderer object', false
, []);
73 $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.');
74 $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.');
75 $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false
, 'default');
79 * Render a given media file
81 * @return string Rendered tag
82 * @throws \UnexpectedValueException
84 public function render()
86 $file = $this->arguments
['file'];
87 $additionalConfig = $this->arguments
['additionalConfig'];
88 $width = $this->arguments
['width'];
89 $height = $this->arguments
['height'];
91 // get Resource Object (non ExtBase version)
92 if (is_callable([$file, 'getOriginalResource'])) {
93 // We have a domain model, so we need to fetch the FAL resource object from there
94 $file = $file->getOriginalResource();
97 if (!($file instanceof FileInterface ||
$file instanceof AbstractFileFolder
)) {
98 throw new \
UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
101 $fileRenderer = RendererRegistry
::getInstance()->getRenderer($file);
103 // Fallback to image when no renderer is found
104 if ($fileRenderer === null
) {
105 return $this->renderImage($file, $width, $height);
107 $additionalConfig = array_merge_recursive($this->arguments
, $additionalConfig);
108 return $fileRenderer->render($file, $width, $height, $additionalConfig);
115 * @param FileInterface $image
116 * @param string $width
117 * @param string $height
118 * @return string Rendered img tag
120 protected function renderImage(FileInterface
$image, $width, $height)
122 $cropVariant = $this->arguments
['cropVariant'] ?
: 'default';
123 $cropString = $image instanceof FileReference ?
$image->getProperty('crop') : '';
124 $cropVariantCollection = CropVariantCollection
::create((string)$cropString);
125 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
126 $processingInstructions = [
129 'crop' => $cropArea->isEmpty() ? null
: $cropArea->makeAbsoluteBasedOnFile($image),
131 $imageService = $this->getImageService();
132 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
133 $imageUri = $imageService->getImageUri($processedImage);
135 if (!$this->tag
->hasAttribute('data-focus-area')) {
136 $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
137 if (!$focusArea->isEmpty()) {
138 $this->tag
->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
141 $this->tag
->addAttribute('src', $imageUri);
142 $this->tag
->addAttribute('width', $processedImage->getProperty('width'));
143 $this->tag
->addAttribute('height', $processedImage->getProperty('height'));
145 $alt = $image->getProperty('alternative');
146 $title = $image->getProperty('title');
148 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
149 if (empty($this->arguments
['alt'])) {
150 $this->tag
->addAttribute('alt', $alt);
152 if (empty($this->arguments
['title']) && $title) {
153 $this->tag
->addAttribute('title', $title);
156 return $this->tag
->render();
160 * Return an instance of ImageService
162 * @return ImageService
164 protected function getImageService()
166 return $this->objectManager
->get(ImageService
::class);