ImageViewHelper.php 6.19 KB
Newer Older
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
1
<?php
Thomas Maroschik's avatar
Thomas Maroschik committed
2
3
namespace TYPO3\CMS\Fluid\ViewHelpers;

4
5
6
7
8
9
10
11
12
13
14
15
/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */
16
17

use TYPO3\CMS\Core\Resource\FileInterface;
Frans Saris's avatar
Frans Saris committed
18
use TYPO3\CMS\Core\Resource\FileReference;
19
20
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;

Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
21
/**
22
 * Resizes a given image (if required) and renders the respective img tag
23
 *
24
 * = Examples =
25
 *
26
27
28
29
30
31
32
33
 * <code title="Default">
 * <f:image src="EXT:myext/Resources/Public/typo3_logo.png" alt="alt text" />
 * </code>
 * <output>
 * <img alt="alt text" src="typo3conf/ext/myext/Resources/Public/typo3_logo.png" width="396" height="375" />
 * or (in BE mode):
 * <img alt="alt text" src="../typo3conf/ext/viewhelpertest/Resources/Public/typo3_logo.png" width="396" height="375" />
 * </output>
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
34
 *
35
36
37
38
39
40
41
 * <code title="Image Object">
 * <f:image image="{imageObject}" />
 * </code>
 * <output>
 * <img alt="alt set in image record" src="fileadmin/_processed_/323223424.png" width="396" height="375" />
 * </output>
 *
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 * <code title="Inline notation">
 * {f:image(src: 'EXT:viewhelpertest/Resources/Public/typo3_logo.png', alt: 'alt text', minWidth: 30, maxWidth: 40)}
 * </code>
 * <output>
 * <img alt="alt text" src="../typo3temp/pics/f13d79a526.png" width="40" height="38" />
 * (depending on your TYPO3s encryption key)
 * </output>
 *
 * <code title="non existing image">
 * <f:image src="NonExistingImage.png" alt="foo" />
 * </code>
 * <output>
 * Could not get image resource for "NonExistingImage.png".
 * </output>
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
56
 */
Thomas Maroschik's avatar
Thomas Maroschik committed
57
class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
58
59
60
61
62
	/**
	 * @var string
	 */
	protected $tagName = 'img';

63
	/**
64
65
	 * @var \TYPO3\CMS\Extbase\Service\ImageService
	 * @inject
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
66
	 */
67
	protected $imageService;
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
68
69
70
71
72
73
74
75
76

	/**
	 * Initialize arguments.
	 *
	 * @return void
	 */
	public function initializeArguments() {
		parent::initializeArguments();
		$this->registerUniversalTagAttributes();
77
		$this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', FALSE);
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
78
79
80
81
82
83
		$this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
		$this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
		$this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
	}

	/**
84
	 * Resizes a given image (if required) and renders the respective img tag
85
	 *
86
	 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/
87
	 * @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
88
89
	 * @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.
	 * @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.
90
91
92
93
	 * @param int $minWidth minimum width of the image
	 * @param int $minHeight minimum height of the image
	 * @param int $maxWidth maximum width of the image
	 * @param int $maxHeight maximum height of the image
94
	 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
95
	 * @param FileInterface|AbstractFileFolder $image a FAL object
Frans Saris's avatar
Frans Saris committed
96
	 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
97
	 *
98
	 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
99
	 * @return string Rendered tag
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
100
	 */
Frans Saris's avatar
Frans Saris committed
101
	public function render($src = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $image = NULL, $crop = NULL) {
102
		if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
103
			throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
104
		}
105
		$image = $this->imageService->getImage($src, $image, $treatIdAsReference);
Frans Saris's avatar
Frans Saris committed
106
107
108
		if ($crop === NULL) {
			$crop = $image instanceof FileReference ? $image->getProperty('crop') : NULL;
		}
109
		$processingInstructions = array(
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
110
111
			'width' => $width,
			'height' => $height,
112
113
114
115
			'minWidth' => $minWidth,
			'minHeight' => $minHeight,
			'maxWidth' => $maxWidth,
			'maxHeight' => $maxHeight,
Frans Saris's avatar
Frans Saris committed
116
			'crop' => $crop,
Bastian Waidelich's avatar
Fluid:  
Bastian Waidelich committed
117
		);
118
119
120
121
122
123
124
125
126
127
128
		$processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
		$imageUri = $this->imageService->getImageUri($processedImage);

		$this->tag->addAttribute('src', $imageUri);
		$this->tag->addAttribute('width', $processedImage->getProperty('width'));
		$this->tag->addAttribute('height', $processedImage->getProperty('height'));

		$alt = $image->getProperty('alternative');
		$title = $image->getProperty('title');

		// The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
129
		if (empty($this->arguments['alt'])) {
130
131
132
133
			$this->tag->addAttribute('alt', $alt);
		}
		if (empty($this->arguments['title']) && $title) {
			$this->tag->addAttribute('title', $title);
134
		}
135

136
		return $this->tag->render();
137
	}
138

139
}