[BUGFIX] Ensure type safety for ViewHelper calling ImageService
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / ImageViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16
17 use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
18 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21
22 /**
23 * Resizes a given image (if required) and renders the respective img tag
24 *
25 * = Examples =
26 *
27 * <code title="Default">
28 * <f:image src="EXT:myext/Resources/Public/typo3_logo.png" alt="alt text" />
29 * </code>
30 * <output>
31 * <img alt="alt text" src="typo3conf/ext/myext/Resources/Public/typo3_logo.png" width="396" height="375" />
32 * or (in BE mode):
33 * <img alt="alt text" src="../typo3conf/ext/viewhelpertest/Resources/Public/typo3_logo.png" width="396" height="375" />
34 * </output>
35 *
36 * <code title="Image Object">
37 * <f:image image="{imageObject}" />
38 * </code>
39 * <output>
40 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
41 * </output>
42 *
43 * <code title="Inline notation">
44 * {f:image(src: 'EXT:viewhelpertest/Resources/Public/typo3_logo.png', alt: 'alt text', minWidth: 30, maxWidth: 40)}
45 * </code>
46 * <output>
47 * <img alt="alt text" src="../typo3temp/assets/images/f13d79a526.png" width="40" height="38" />
48 * (depending on your TYPO3s encryption key)
49 * </output>
50 *
51 * <code title="Other resource type (e.g. PDF)">
52 * <f:image src="fileadmin/user_upload/example.pdf" alt="foo" />
53 * </code>
54 * <output>
55 * If your graphics processing library is set up correctly then it will output a thumbnail of the first page of your PDF document.
56 * <img src="fileadmin/_processed_/1/2/csm_example_aabbcc112233.gif" width="200" height="284" alt="foo">
57 * </output>
58 *
59 * <code title="Non-existent image">
60 * <f:image src="NonExistingImage.png" alt="foo" />
61 * </code>
62 * <output>
63 * Could not get image resource for "NonExistingImage.png".
64 * </output>
65 */
66 class ImageViewHelper extends AbstractTagBasedViewHelper
67 {
68 /**
69 * @var string
70 */
71 protected $tagName = 'img';
72
73 /**
74 * @var \TYPO3\CMS\Extbase\Service\ImageService
75 */
76 protected $imageService;
77
78 /**
79 * @param \TYPO3\CMS\Extbase\Service\ImageService $imageService
80 */
81 public function injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
82 {
83 $this->imageService = $imageService;
84 }
85
86 /**
87 * Initialize arguments.
88 */
89 public function initializeArguments()
90 {
91 parent::initializeArguments();
92 $this->registerUniversalTagAttributes();
93 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
94 $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
95 $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
96 $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
97
98 $this->registerArgument('src', 'string', 'a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead', false, '');
99 $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
100 $this->registerArgument('image', 'object', 'a FAL object');
101 $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
102 $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
103
104 $this->registerArgument('width', 'string', 'width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
105 $this->registerArgument('height', 'string', 'height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
106 $this->registerArgument('minWidth', 'int', 'minimum width of the image');
107 $this->registerArgument('minHeight', 'int', 'minimum width of the image');
108 $this->registerArgument('maxWidth', 'int', 'minimum width of the image');
109 $this->registerArgument('maxHeight', 'int', 'minimum width of the image');
110 $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
111 }
112
113 /**
114 * Resizes a given image (if required) and renders the respective img tag
115 *
116 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
117 *
118 * @throws Exception
119 * @return string Rendered tag
120 */
121 public function render()
122 {
123 if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) {
124 throw new Exception('You must either specify a string src or a File object.', 1382284106);
125 }
126
127 try {
128 $image = $this->imageService->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
129 $cropString = $this->arguments['crop'];
130 if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
131 $cropString = $image->getProperty('crop');
132 }
133 $cropVariantCollection = CropVariantCollection::create((string)$cropString);
134 $cropVariant = $this->arguments['cropVariant'] ?: 'default';
135 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
136 $processingInstructions = [
137 'width' => $this->arguments['width'],
138 'height' => $this->arguments['height'],
139 'minWidth' => $this->arguments['minWidth'],
140 'minHeight' => $this->arguments['minHeight'],
141 'maxWidth' => $this->arguments['maxWidth'],
142 'maxHeight' => $this->arguments['maxHeight'],
143 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
144 ];
145 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
146 $imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);
147
148 if (!$this->tag->hasAttribute('data-focus-area')) {
149 $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
150 if (!$focusArea->isEmpty()) {
151 $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
152 }
153 }
154 $this->tag->addAttribute('src', $imageUri);
155 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
156 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
157
158 $alt = $image->getProperty('alternative');
159 $title = $image->getProperty('title');
160
161 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
162 if (empty($this->arguments['alt'])) {
163 $this->tag->addAttribute('alt', $alt);
164 }
165 if (empty($this->arguments['title']) && $title) {
166 $this->tag->addAttribute('title', $title);
167 }
168 } catch (ResourceDoesNotExistException $e) {
169 // thrown if file does not exist
170 throw new Exception($e->getMessage(), 1509741911, $e);
171 } catch (\UnexpectedValueException $e) {
172 // thrown if a file has been replaced with a folder
173 throw new Exception($e->getMessage(), 1509741912, $e);
174 } catch (\RuntimeException $e) {
175 // RuntimeException thrown if a file is outside of a storage
176 throw new Exception($e->getMessage(), 1509741913, $e);
177 } catch (\InvalidArgumentException $e) {
178 // thrown if file storage does not exist
179 throw new Exception($e->getMessage(), 1509741914, $e);
180 }
181
182 return $this->tag->render();
183 }
184 }