[TASK] Make Uri/ImageViewHelper compilable
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / ImageViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
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\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
21 use TYPO3\CMS\Extbase\Object\ObjectManager;
22 use TYPO3\CMS\Extbase\Service\ImageService;
23 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
25 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
26 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
27
28 /**
29 * Resizes a given image (if required) and returns its relative path.
30 *
31 * = Examples =
32 *
33 * <code title="Default">
34 * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
35 * </code>
36 * <output>
37 * typo3conf/ext/myext/Resources/Public/typo3_logo.png
38 * or (in BE mode):
39 * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
40 * </output>
41 *
42 * <code title="Image Object">
43 * <f:uri.image image="{imageObject}" />
44 * </code>
45 * <output>
46 * fileadmin/images/image.png
47 * or (in BE mode):
48 * fileadmin/images/image.png
49 * </output>
50 *
51 * <code title="Inline notation">
52 * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png', minWidth: 30, maxWidth: 40)}
53 * </code>
54 * <output>
55 * typo3temp/pics/[b4c0e7ed5c].png
56 * (depending on your TYPO3s encryption key)
57 * </output>
58 *
59 * <code title="non existing image">
60 * <f:uri.image src="NonExistingImage.png" />
61 * </code>
62 * <output>
63 * Could not get image resource for "NonExistingImage.png".
64 * </output>
65 */
66 class ImageViewHelper extends AbstractViewHelper implements CompilableInterface {
67
68 /**
69 * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
70 *
71 * @see http://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/ImgResource/
72 * @param string $src
73 * @param FileInterface|AbstractFileFolder $image
74 * @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.
75 * @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 * @param int $minWidth minimum width of the image
77 * @param int $minHeight minimum height of the image
78 * @param int $maxWidth maximum width of the image
79 * @param int $maxHeight maximum height of the image
80 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
81 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
82 * @throws Exception
83 * @return string path to the image
84 */
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 return self::renderStatic(
87 array(
88 'src' => $src,
89 'image' => $image,
90 'width' => $width,
91 'height' => $height,
92 'minWidth' => $minWidth,
93 'minHeight' => $minHeight,
94 'maxWidth' => $maxWidth,
95 'maxHeight' => $maxHeight,
96 'treatIdAsReference' => $treatIdAsReference,
97 'crop' => $crop,
98 ),
99 $this->buildRenderChildrenClosure(),
100 $this->renderingContext
101 );
102 }
103
104 /**
105 * @param array $arguments
106 * @param callable $renderChildrenClosure
107 * @param RenderingContextInterface $renderingContext
108 * @return string
109 */
110 static public function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext) {
111 $src = $arguments['src'];
112 $image = $arguments['image'];
113 $treatIdAsReference = $arguments['treatIdAsReference'];
114 $crop = $arguments['crop'];
115
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 if ($crop === NULL) {
120 $crop = $image instanceof FileReference ? $image->getProperty('crop') : NULL;
121 }
122
123 $imageService = self::getImageService();
124
125 $image = $imageService->getImage($src, $image, $treatIdAsReference);
126 $processingInstructions = array(
127 'width' => $arguments['width'],
128 'height' => $arguments['height'],
129 'minWidth' => $arguments['minWidth'],
130 'minHeight' => $arguments['minHeight'],
131 'maxWidth' => $arguments['maxWidth'],
132 'maxHeight' => $arguments['maxHeight'],
133 'crop' => $crop,
134 );
135 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
136 return $imageService->getImageUri($processedImage);
137 }
138
139 /**
140 * Return an instance of ImageService using object manager
141 *
142 * @return ImageService
143 */
144 static protected function getImageService() {
145 /** @var ObjectManager $objectManager */
146 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
147 return $objectManager->get(ImageService::class);
148 }
149
150 }