78a12f48871dcc00c7e087d63b5309020198670e
[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\FileInterface;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
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/pics/f13d79a526.png" width="40" height="38" />
48 * (depending on your TYPO3s encryption key)
49 * </output>
50 *
51 * <code title="non existing image">
52 * <f:image src="NonExistingImage.png" alt="foo" />
53 * </code>
54 * <output>
55 * Could not get image resource for "NonExistingImage.png".
56 * </output>
57 */
58 class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
59 {
60 /**
61 * @var string
62 */
63 protected $tagName = 'img';
64
65 /**
66 * @var \TYPO3\CMS\Extbase\Service\ImageService
67 * @inject
68 */
69 protected $imageService;
70
71 /**
72 * Initialize arguments.
73 *
74 * @return void
75 */
76 public function initializeArguments()
77 {
78 parent::initializeArguments();
79 $this->registerUniversalTagAttributes();
80 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
81 $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
82 $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
83 $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
84 }
85
86 /**
87 * Resizes a given image (if required) and renders the respective img tag
88 *
89 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
90 * @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
91 * @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.
92 * @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.
93 * @param int $minWidth minimum width of the image
94 * @param int $minHeight minimum height of the image
95 * @param int $maxWidth maximum width of the image
96 * @param int $maxHeight maximum height of the image
97 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
98 * @param FileInterface|AbstractFileFolder $image a FAL object
99 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
100 * @param bool $absolute Force absolute URL
101 *
102 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
103 * @return string Rendered tag
104 */
105 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)
106 {
107 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
108 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
109 }
110
111 try {
112 $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
113 if ($crop === null) {
114 $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
115 }
116 $processingInstructions = array(
117 'width' => $width,
118 'height' => $height,
119 'minWidth' => $minWidth,
120 'minHeight' => $minHeight,
121 'maxWidth' => $maxWidth,
122 'maxHeight' => $maxHeight,
123 'crop' => $crop,
124 );
125 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
126 $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
127
128 $this->tag->addAttribute('src', $imageUri);
129 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
130 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
131
132 $alt = $image->getProperty('alternative');
133 $title = $image->getProperty('title');
134
135 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
136 if (empty($this->arguments['alt'])) {
137 $this->tag->addAttribute('alt', $alt);
138 }
139 if (empty($this->arguments['title']) && $title) {
140 $this->tag->addAttribute('title', $title);
141 }
142 } catch (ResourceDoesNotExistException $e) {
143 // thrown if file does not exist
144 } catch (\UnexpectedValueException $e) {
145 // thrown if a file has been replaced with a folder
146 } catch (\RuntimeException $e) {
147 // RuntimeException thrown if a file is outside of a storage
148 } catch (\InvalidArgumentException $e) {
149 // thrown if file storage does not exist
150 }
151
152 return $this->tag->render();
153 }
154 }