513496b83b19c1a660c4855588090594f82561a8
[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\Rendering\RenderingContextInterface;
24 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
25 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
26 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
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 https://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 * @param bool $absolute Force absolute URL
83 * @throws Exception
84 * @return string path to the image
85 */
86 public function render($src = null, $image = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $crop = null, $absolute = false)
87 {
88 return self::renderStatic(
89 array(
90 'src' => $src,
91 'image' => $image,
92 'width' => $width,
93 'height' => $height,
94 'minWidth' => $minWidth,
95 'minHeight' => $minHeight,
96 'maxWidth' => $maxWidth,
97 'maxHeight' => $maxHeight,
98 'treatIdAsReference' => $treatIdAsReference,
99 'crop' => $crop,
100 'absolute' => $absolute,
101 ),
102 $this->buildRenderChildrenClosure(),
103 $this->renderingContext
104 );
105 }
106
107 /**
108 * @param array $arguments
109 * @param callable $renderChildrenClosure
110 * @param RenderingContextInterface $renderingContext
111 * @return string
112 * @throws Exception
113 */
114 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
115 {
116 $src = $arguments['src'];
117 $image = $arguments['image'];
118 $treatIdAsReference = $arguments['treatIdAsReference'];
119 $crop = $arguments['crop'];
120 $absolute = $arguments['absolute'];
121
122 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
123 throw new Exception('You must either specify a string src or a File object.', 1382284105);
124 }
125
126 $imageService = self::getImageService();
127 $image = $imageService->getImage($src, $image, $treatIdAsReference);
128
129 if ($crop === null) {
130 $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
131 }
132
133 $processingInstructions = array(
134 'width' => $arguments['width'],
135 'height' => $arguments['height'],
136 'minWidth' => $arguments['minWidth'],
137 'minHeight' => $arguments['minHeight'],
138 'maxWidth' => $arguments['maxWidth'],
139 'maxHeight' => $arguments['maxHeight'],
140 'crop' => $crop,
141 );
142 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
143 return $imageService->getImageUri($processedImage, $absolute);
144 }
145
146 /**
147 * Return an instance of ImageService using object manager
148 *
149 * @return ImageService
150 */
151 protected static function getImageService()
152 {
153 /** @var ObjectManager $objectManager */
154 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
155 return $objectManager->get(ImageService::class);
156 }
157 }