[TASK] Move publicly accessible files to typo3temp/assets/
[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\Exception\ResourceDoesNotExistException;
18 use TYPO3\CMS\Core\Resource\FileInterface;
19 use TYPO3\CMS\Core\Resource\FileReference;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
23 use TYPO3\CMS\Extbase\Service\ImageService;
24 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
25 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
26 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception;
27 use TYPO3\CMS\Fluid\Core\ViewHelper\Facets\CompilableInterface;
28
29 /**
30 * Resizes a given image (if required) and returns its relative path.
31 *
32 * = Examples =
33 *
34 * <code title="Default">
35 * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
36 * </code>
37 * <output>
38 * typo3conf/ext/myext/Resources/Public/typo3_logo.png
39 * or (in BE mode):
40 * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
41 * </output>
42 *
43 * <code title="Image Object">
44 * <f:uri.image image="{imageObject}" />
45 * </code>
46 * <output>
47 * fileadmin/images/image.png
48 * or (in BE mode):
49 * fileadmin/images/image.png
50 * </output>
51 *
52 * <code title="Inline notation">
53 * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png', minWidth: 30, maxWidth: 40)}
54 * </code>
55 * <output>
56 * typo3temp/assets/images/[b4c0e7ed5c].png
57 * (depending on your TYPO3s encryption key)
58 * </output>
59 *
60 * <code title="non existing image">
61 * <f:uri.image src="NonExistingImage.png" />
62 * </code>
63 * <output>
64 * Could not get image resource for "NonExistingImage.png".
65 * </output>
66 */
67 class ImageViewHelper extends AbstractViewHelper implements CompilableInterface
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 *
72 * @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/ImgResource/
73 * @param string $src
74 * @param FileInterface|AbstractFileFolder $image
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.
76 * @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.
77 * @param int $minWidth minimum width of the image
78 * @param int $minHeight minimum height of the image
79 * @param int $maxWidth maximum width of the image
80 * @param int $maxHeight maximum height of the image
81 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
82 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
83 * @param bool $absolute Force absolute URL
84 * @throws Exception
85 * @return string path to the image
86 */
87 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)
88 {
89 return self::renderStatic(
90 array(
91 'src' => $src,
92 'image' => $image,
93 'width' => $width,
94 'height' => $height,
95 'minWidth' => $minWidth,
96 'minHeight' => $minHeight,
97 'maxWidth' => $maxWidth,
98 'maxHeight' => $maxHeight,
99 'treatIdAsReference' => $treatIdAsReference,
100 'crop' => $crop,
101 'absolute' => $absolute,
102 ),
103 $this->buildRenderChildrenClosure(),
104 $this->renderingContext
105 );
106 }
107
108 /**
109 * @param array $arguments
110 * @param callable $renderChildrenClosure
111 * @param RenderingContextInterface $renderingContext
112 * @return string
113 * @throws Exception
114 */
115 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
116 {
117 $src = $arguments['src'];
118 $image = $arguments['image'];
119 $treatIdAsReference = $arguments['treatIdAsReference'];
120 $crop = $arguments['crop'];
121 $absolute = $arguments['absolute'];
122
123 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
124 throw new Exception('You must either specify a string src or a File object.', 1382284105);
125 }
126
127 try {
128 $imageService = self::getImageService();
129 $image = $imageService->getImage($src, $image, $treatIdAsReference);
130
131 if ($crop === null) {
132 $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
133 }
134
135 $processingInstructions = array(
136 'width' => $arguments['width'],
137 'height' => $arguments['height'],
138 'minWidth' => $arguments['minWidth'],
139 'minHeight' => $arguments['minHeight'],
140 'maxWidth' => $arguments['maxWidth'],
141 'maxHeight' => $arguments['maxHeight'],
142 'crop' => $crop,
143 );
144 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
145 return $imageService->getImageUri($processedImage, $absolute);
146 } catch (ResourceDoesNotExistException $e) {
147 // thrown if file does not exist
148 } catch (\UnexpectedValueException $e) {
149 // thrown if a file has been replaced with a folder
150 } catch (\RuntimeException $e) {
151 // RuntimeException thrown if a file is outside of a storage
152 } catch (\InvalidArgumentException $e) {
153 // thrown if file storage does not exist
154 }
155 return '';
156 }
157
158 /**
159 * Return an instance of ImageService using object manager
160 *
161 * @return ImageService
162 */
163 protected static function getImageService()
164 {
165 /** @var ObjectManager $objectManager */
166 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
167 return $objectManager->get(ImageService::class);
168 }
169 }