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