[FEATURE] Add MediaViewHelper
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / MediaViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
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
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Core\Resource\FileReference;
19 use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry;
20 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
21 use TYPO3\CMS\Extbase\Service\ImageService;
22 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
23
24 /**
25 * Render a given media file with the correct html tag.
26 *
27 * It asks the RendererRegister for the correct Renderer class and if not found it falls
28 * back to the ImageViewHelper as that is the "Renderer" class for images in Fluid context.
29 *
30 * = Examples =
31 *
32 * <code title="Image Object">
33 * <f:media file="{file}" width="400" height="375" />
34 * </code>
35 * <output>
36 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
37 * </output>
38 *
39 * <code title="MP4 Video Object">
40 * <f:media file="{file}" width="400" height="375" />
41 * </code>
42 * <output>
43 * <video width="400" height="375" controls><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
44 * </output>
45 *
46 * <code title="MP4 Video Object with loop and autoplay option set">
47 * <f:media file="{file}" width="400" height="375" additionalConfig="{loop: '1', autoplay: '1'}" />
48 * </code>
49 * <output>
50 * <video width="400" height="375" controls loop><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video>
51 * </output>
52 */
53 class MediaViewHelper extends AbstractTagBasedViewHelper {
54
55 /**
56 * @var string
57 */
58 protected $tagName = 'img';
59
60 /**
61 * Initialize arguments.
62 *
63 * @return void
64 */
65 public function initializeArguments() {
66 parent::initializeArguments();
67 $this->registerUniversalTagAttributes();
68 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', FALSE);
69 }
70
71 /**
72 * Render a given media file
73 *
74 * @param FileInterface|AbstractFileFolder $file
75 * @param array $additionalConfig This array can hold additional configuration that is passed though to the Renderer object
76 * @param string $width 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.
77 * @param string $height 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.
78 * @return string Rendered tag
79 */
80 public function render($file, $additionalConfig = array(), $width = NULL, $height = NULL) {
81
82 // get Resource Object (non ExtBase version)
83 if (is_callable(array($file, 'getOriginalResource'))) {
84 // We have a domain model, so we need to fetch the FAL resource object from there
85 $file = $file->getOriginalResource();
86 }
87
88 $fileRenderer = RendererRegistry::getInstance()->getRenderer($file);
89
90 // Fallback to image when no renderer is found
91 if ($fileRenderer === NULL) {
92 return $this->renderImage($file, $width, $height);
93 } else {
94 $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
95 return $fileRenderer->render($file, $width, $height, $additionalConfig);
96 }
97 }
98
99 /**
100 * Render img tag
101 *
102 * @param FileInterface $image
103 * @param string $width
104 * @param string $height
105 * @return string Rendered img tag
106 */
107 protected function renderImage(FileInterface $image, $width, $height) {
108 $crop = $image instanceof FileReference ? $image->getProperty('crop') : NULL;
109 $processingInstructions = array(
110 'width' => $width,
111 'height' => $height,
112 'crop' => $crop,
113 );
114 $imageService = $this->getImageService();
115 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
116 $imageUri = $imageService->getImageUri($processedImage);
117
118 $this->tag->addAttribute('src', $imageUri);
119 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
120 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
121
122 $alt = $image->getProperty('alternative');
123 $title = $image->getProperty('title');
124
125 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
126 if (empty($this->arguments['alt'])) {
127 $this->tag->addAttribute('alt', $alt);
128 }
129 if (empty($this->arguments['title']) && $title) {
130 $this->tag->addAttribute('title', $title);
131 }
132
133 return $this->tag->render();
134 }
135
136 /**
137 * Return an instance of ImageService
138 *
139 * @return ImageService
140 */
141 protected function getImageService() {
142 return $this->objectManager->get(ImageService::class);
143 }
144 }