|
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
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Fluid\ViewHelpers; |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; |
|
19
|
|
|
use TYPO3\CMS\Core\Resource\FileInterface; |
|
20
|
|
|
use TYPO3\CMS\Core\Resource\FileReference; |
|
21
|
|
|
use TYPO3\CMS\Core\Resource\Rendering\RendererRegistry; |
|
22
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
23
|
|
|
use TYPO3\CMS\Extbase\Service\ImageService; |
|
24
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; |
|
25
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Render a given media file with the correct html tag. |
|
29
|
|
|
* |
|
30
|
|
|
* It asks the :php:`RendererRegistry` for the correct Renderer class and if not found it falls |
|
31
|
|
|
* back to the :php:`ImageViewHelper` as that is the "Renderer" class for images in Fluid context. |
|
32
|
|
|
* |
|
33
|
|
|
* Examples |
|
34
|
|
|
* ======== |
|
35
|
|
|
* |
|
36
|
|
|
* Image Object |
|
37
|
|
|
* ------------ |
|
38
|
|
|
* |
|
39
|
|
|
* :: |
|
40
|
|
|
* |
|
41
|
|
|
* <f:media file="{file}" width="400" height="375" /> |
|
42
|
|
|
* |
|
43
|
|
|
* Output:: |
|
44
|
|
|
* |
|
45
|
|
|
* <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" /> |
|
46
|
|
|
* |
|
47
|
|
|
* MP4 Video Object |
|
48
|
|
|
* ---------------- |
|
49
|
|
|
* |
|
50
|
|
|
* :: |
|
51
|
|
|
* |
|
52
|
|
|
* <f:media file="{file}" width="400" height="375" /> |
|
53
|
|
|
* |
|
54
|
|
|
* Output:: |
|
55
|
|
|
* |
|
56
|
|
|
* <video width="400" height="375" controls><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video> |
|
57
|
|
|
* |
|
58
|
|
|
* MP4 Video Object with loop and autoplay option set |
|
59
|
|
|
* -------------------------------------------------- |
|
60
|
|
|
* |
|
61
|
|
|
* :: |
|
62
|
|
|
* |
|
63
|
|
|
* <f:media file="{file}" width="400" height="375" additionalConfig="{loop: '1', autoplay: '1'}" /> |
|
64
|
|
|
* |
|
65
|
|
|
* Output:: |
|
66
|
|
|
* |
|
67
|
|
|
* <video width="400" height="375" controls loop><source src="fileadmin/user_upload/my-video.mp4" type="video/mp4"></video> |
|
68
|
|
|
*/ |
|
69
|
|
|
class MediaViewHelper extends AbstractTagBasedViewHelper |
|
70
|
|
|
{ |
|
71
|
|
|
/** |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $tagName = 'img'; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Initialize arguments. |
|
78
|
|
|
*/ |
|
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
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Render a given media file |
|
96
|
|
|
* |
|
97
|
|
|
* @return string Rendered tag |
|
98
|
|
|
* @throws \UnexpectedValueException |
|
99
|
|
|
*/ |
|
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 = RendererRegistry::getInstance()->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
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Render img tag |
|
133
|
|
|
* |
|
134
|
|
|
* @param FileInterface $image |
|
135
|
|
|
* @param string $width |
|
136
|
|
|
* @param string $height |
|
137
|
|
|
* @param string|null $fileExtension |
|
138
|
|
|
* @return string Rendered img tag |
|
139
|
|
|
*/ |
|
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
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Return an instance of ImageService |
|
190
|
|
|
* |
|
191
|
|
|
* @return ImageService |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function getImageService() |
|
194
|
|
|
{ |
|
195
|
|
|
return GeneralUtility::makeInstance(ImageService::class); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|