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