01ed916c3394210ba5882517c52d5b48664a3379
[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\Resource\Exception\ResourceDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\FileReference;
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-existant 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 * @return void
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
99 /**
100 * Resizes a given image (if required) and renders the respective img tag
101 *
102 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
103 * @param string $src 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
104 * @param string $width 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 * @param string $height 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 * @param int $minWidth minimum width of the image
107 * @param int $minHeight minimum height of the image
108 * @param int $maxWidth maximum width of the image
109 * @param int $maxHeight maximum height of the image
110 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
111 * @param object $image a FAL object
112 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
113 * @param bool $absolute Force absolute URL
114 *
115 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
116 * @return string Rendered tag
117 */
118 public function render($src = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $image = null, $crop = null, $absolute = false)
119 {
120 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
121 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
122 }
123
124 try {
125 $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
126 if ($crop === null) {
127 $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
128 }
129 $processingInstructions = [
130 'width' => $width,
131 'height' => $height,
132 'minWidth' => $minWidth,
133 'minHeight' => $minHeight,
134 'maxWidth' => $maxWidth,
135 'maxHeight' => $maxHeight,
136 'crop' => $crop,
137 ];
138 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
139 $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
140
141 $this->tag->addAttribute('src', $imageUri);
142 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
143 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
144
145 $alt = $image->getProperty('alternative');
146 $title = $image->getProperty('title');
147
148 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
149 if (empty($this->arguments['alt'])) {
150 $this->tag->addAttribute('alt', $alt);
151 }
152 if (empty($this->arguments['title']) && $title) {
153 $this->tag->addAttribute('title', $title);
154 }
155 } catch (ResourceDoesNotExistException $e) {
156 // thrown if file does not exist
157 } catch (\UnexpectedValueException $e) {
158 // thrown if a file has been replaced with a folder
159 } catch (\RuntimeException $e) {
160 // RuntimeException thrown if a file is outside of a storage
161 } catch (\InvalidArgumentException $e) {
162 // thrown if file storage does not exist
163 }
164
165 return $this->tag->render();
166 }
167 }