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

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;
18
use TYPO3\CMS\Core\Resource\FileReference;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
21
22
23
24
25
26
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
27

Wouter Wolters's avatar
Wouter Wolters committed
28
/**
29
 * Resizes a given image (if required) and returns its relative path.
30
 *
31
 * = Examples =
32
 *
33
34
35
36
37
38
39
40
41
 * <code title="Default">
 * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
 * </code>
 * <output>
 * typo3conf/ext/myext/Resources/Public/typo3_logo.png
 * or (in BE mode):
 * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
 * </output>
 *
42
43
44
45
46
47
48
49
50
 * <code title="Image Object">
 * <f:uri.image image="{imageObject}" />
 * </code>
 * <output>
 * fileadmin/images/image.png
 * or (in BE mode):
 * fileadmin/images/image.png
 * </output>
 *
51
 * <code title="Inline notation">
52
 * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png', minWidth: 30, maxWidth: 40)}
53
54
55
56
57
58
59
60
61
62
63
64
 * </code>
 * <output>
 * typo3temp/pics/[b4c0e7ed5c].png
 * (depending on your TYPO3s encryption key)
 * </output>
 *
 * <code title="non existing image">
 * <f:uri.image src="NonExistingImage.png" />
 * </code>
 * <output>
 * Could not get image resource for "NonExistingImage.png".
 * </output>
65
 */
66
class ImageViewHelper extends AbstractViewHelper  implements CompilableInterface {
67
68
69
70

	/**
	 * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
	 *
71
	 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/ImgResource/
72
	 * @param string $src
73
	 * @param FileInterface|AbstractFileFolder $image
74
75
	 * @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.
76
77
78
79
	 * @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
80
	 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
Frans Saris's avatar
Frans Saris committed
81
	 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
82
	 * @throws Exception
83
84
	 * @return string path to the image
	 */
Frans Saris's avatar
Frans Saris committed
85
	public function render($src = NULL, $image = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $crop = NULL) {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
		return self::renderStatic(
			array(
				'src' => $src,
				'image' => $image,
				'width' => $width,
				'height' => $height,
				'minWidth' => $minWidth,
				'minHeight' => $minHeight,
				'maxWidth' => $maxWidth,
				'maxHeight' => $maxHeight,
				'treatIdAsReference' => $treatIdAsReference,
				'crop' => $crop,
			),
			$this->buildRenderChildrenClosure(),
			$this->renderingContext
		);
	}

	/**
	 * @param array $arguments
	 * @param callable $renderChildrenClosure
	 * @param RenderingContextInterface $renderingContext
	 * @return string
	 */
	static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
		$src = $arguments['src'];
		$image = $arguments['image'];
		$treatIdAsReference = $arguments['treatIdAsReference'];
		$crop = $arguments['crop'];

116
		if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
117
			throw new Exception('You must either specify a string src or a File object.', 1382284105);
118
		}
119
120
121
122

		$imageService = self::getImageService();
		$image = $imageService->getImage($src, $image, $treatIdAsReference);

Frans Saris's avatar
Frans Saris committed
123
124
125
		if ($crop === NULL) {
			$crop = $image instanceof FileReference ? $image->getProperty('crop') : NULL;
		}
126

127
		$processingInstructions = array(
128
129
130
131
132
133
			'width' => $arguments['width'],
			'height' => $arguments['height'],
			'minWidth' => $arguments['minWidth'],
			'minHeight' => $arguments['minHeight'],
			'maxWidth' => $arguments['maxWidth'],
			'maxHeight' => $arguments['maxHeight'],
Frans Saris's avatar
Frans Saris committed
134
			'crop' => $crop,
135
		);
136
137
138
139
140
141
142
143
144
145
146
147
148
		$processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
		return $imageService->getImageUri($processedImage);
	}

	/**
	 * Return an instance of ImageService using object manager
	 *
	 * @return ImageService
	 */
	static protected function getImageService() {
		/** @var ObjectManager $objectManager */
		$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
		return $objectManager->get(ImageService::class);
149
	}
150

151
}