[TASK] Do not silently ignore exceptions in ImageViewhelper
[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\Exception;
20
21 /**
22 * Resizes a given image (if required) and renders the respective img tag
23 *
24 * = Examples =
25 *
26 * <code title="Default">
27 * <f:image src="EXT:myext/Resources/Public/typo3_logo.png" alt="alt text" />
28 * </code>
29 * <output>
30 * <img alt="alt text" src="typo3conf/ext/myext/Resources/Public/typo3_logo.png" width="396" height="375" />
31 * or (in BE mode):
32 * <img alt="alt text" src="../typo3conf/ext/viewhelpertest/Resources/Public/typo3_logo.png" width="396" height="375" />
33 * </output>
34 *
35 * <code title="Image Object">
36 * <f:image image="{imageObject}" />
37 * </code>
38 * <output>
39 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
40 * </output>
41 *
42 * <code title="Inline notation">
43 * {f:image(src: 'EXT:viewhelpertest/Resources/Public/typo3_logo.png', alt: 'alt text', minWidth: 30, maxWidth: 40)}
44 * </code>
45 * <output>
46 * <img alt="alt text" src="../typo3temp/assets/images/f13d79a526.png" width="40" height="38" />
47 * (depending on your TYPO3s encryption key)
48 * </output>
49 *
50 * <code title="Other resource type (e.g. PDF)">
51 * <f:image src="fileadmin/user_upload/example.pdf" alt="foo" />
52 * </code>
53 * <output>
54 * If your graphics processing library is set up correctly then it will output a thumbnail of the first page of your PDF document.
55 * <img src="fileadmin/_processed_/1/2/csm_example_aabbcc112233.gif" width="200" height="284" alt="foo">
56 * </output>
57 *
58 * <code title="Non-existent image">
59 * <f:image src="NonExistingImage.png" alt="foo" />
60 * </code>
61 * <output>
62 * Could not get image resource for "NonExistingImage.png".
63 * </output>
64 */
65 class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
66 {
67 /**
68 * @var string
69 */
70 protected $tagName = 'img';
71
72 /**
73 * @var \TYPO3\CMS\Extbase\Service\ImageService
74 */
75 protected $imageService;
76
77 /**
78 * @param \TYPO3\CMS\Extbase\Service\ImageService $imageService
79 */
80 public function injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
81 {
82 $this->imageService = $imageService;
83 }
84
85 /**
86 * Initialize arguments.
87 */
88 public function initializeArguments()
89 {
90 parent::initializeArguments();
91 $this->registerUniversalTagAttributes();
92 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
93 $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
94 $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
95 $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
96
97 $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');
98 $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record');
99 $this->registerArgument('image', 'object', 'a FAL object');
100 $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
101 $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
102
103 $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.');
104 $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.');
105 $this->registerArgument('minWidth', 'int', 'minimum width of the image');
106 $this->registerArgument('minHeight', 'int', 'minimum width of the image');
107 $this->registerArgument('maxWidth', 'int', 'minimum width of the image');
108 $this->registerArgument('maxHeight', 'int', 'minimum width of the image');
109 $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
110 }
111
112 /**
113 * Resizes a given image (if required) and renders the respective img tag
114 *
115 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
116 *
117 * @throws Exception
118 * @return string Rendered tag
119 */
120 public function render()
121 {
122 if ((is_null($this->arguments['src']) && is_null($this->arguments['image'])) || (!is_null($this->arguments['src']) && !is_null($this->arguments['image']))) {
123 throw new Exception('You must either specify a string src or a File object.', 1382284106);
124 }
125
126 try {
127 $image = $this->imageService->getImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
128 $cropString = $this->arguments['crop'];
129 if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
130 $cropString = $image->getProperty('crop');
131 }
132 $cropVariantCollection = CropVariantCollection::create((string)$cropString);
133 $cropVariant = $this->arguments['cropVariant'] ?: 'default';
134 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
135 $processingInstructions = [
136 'width' => $this->arguments['width'],
137 'height' => $this->arguments['height'],
138 'minWidth' => $this->arguments['minWidth'],
139 'minHeight' => $this->arguments['minHeight'],
140 'maxWidth' => $this->arguments['maxWidth'],
141 'maxHeight' => $this->arguments['maxHeight'],
142 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
143 ];
144 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
145 $imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);
146
147 if (!$this->tag->hasAttribute('data-focus-area')) {
148 $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
149 if (!$focusArea->isEmpty()) {
150 $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
151 }
152 }
153 $this->tag->addAttribute('src', $imageUri);
154 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
155 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
156
157 $alt = $image->getProperty('alternative');
158 $title = $image->getProperty('title');
159
160 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
161 if (empty($this->arguments['alt'])) {
162 $this->tag->addAttribute('alt', $alt);
163 }
164 if (empty($this->arguments['title']) && $title) {
165 $this->tag->addAttribute('title', $title);
166 }
167 } catch (ResourceDoesNotExistException $e) {
168 // thrown if file does not exist
169 throw new Exception($e->getMessage(), 1509741911, $e);
170 } catch (\UnexpectedValueException $e) {
171 // thrown if a file has been replaced with a folder
172 throw new Exception($e->getMessage(), 1509741912, $e);
173 } catch (\RuntimeException $e) {
174 // RuntimeException thrown if a file is outside of a storage
175 throw new Exception($e->getMessage(), 1509741913, $e);
176 } catch (\InvalidArgumentException $e) {
177 // thrown if file storage does not exist
178 throw new Exception($e->getMessage(), 1509741914, $e);
179 }
180
181 return $this->tag->render();
182 }
183 }