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