Revert "[CLEANUP] Rework/simplify copyright header and remove @package"
[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\Extbase\Domain\Model\AbstractFileFolder;
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/pics/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 * @var string
59 */
60 protected $tagName = 'img';
61
62 /**
63 * @var \TYPO3\CMS\Extbase\Service\ImageService
64 * @inject
65 */
66 protected $imageService;
67
68 /**
69 * Initialize arguments.
70 *
71 * @return void
72 */
73 public function initializeArguments() {
74 parent::initializeArguments();
75 $this->registerUniversalTagAttributes();
76 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', FALSE);
77 $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
78 $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
79 $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
80 }
81
82 /**
83 * Resizes a given image (if required) and renders the respective img tag
84 *
85 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
86 * @param string $src a path to a file, a combined FAL identifier or an uid (integer). 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
87 * @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.
88 * @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.
89 * @param integer $minWidth minimum width of the image
90 * @param integer $minHeight minimum height of the image
91 * @param integer $maxWidth maximum width of the image
92 * @param integer $maxHeight maximum height of the image
93 * @param boolean $treatIdAsReference given src argument is a sys_file_reference record
94 * @param FileInterface|AbstractFileFolder $image a FAL object
95 *
96 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
97 * @return string Rendered tag
98 */
99 public function render($src = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $image = NULL) {
100 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
101 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
102 }
103 $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
104 $processingInstructions = array(
105 'width' => $width,
106 'height' => $height,
107 'minWidth' => $minWidth,
108 'minHeight' => $minHeight,
109 'maxWidth' => $maxWidth,
110 'maxHeight' => $maxHeight,
111 );
112 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
113 $imageUri = $this->imageService->getImageUri($processedImage);
114
115 $this->tag->addAttribute('src', $imageUri);
116 $this->tag->addAttribute('width', $processedImage->getProperty('width'));
117 $this->tag->addAttribute('height', $processedImage->getProperty('height'));
118
119 $alt = $image->getProperty('alternative');
120 $title = $image->getProperty('title');
121
122 // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
123 if (empty($this->arguments['alt'])) {
124 $this->tag->addAttribute('alt', $alt);
125 }
126 if (empty($this->arguments['title']) && $title) {
127 $this->tag->addAttribute('title', $title);
128 }
129
130 return $this->tag->render();
131 }
132 }