[BUGFIX] Remove unreachable return statement
[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\Imaging\ImageManipulation\CropVariantCollection;
18 use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Extbase\Object\ObjectManager;
21 use TYPO3\CMS\Extbase\Service\ImageService;
22 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
24 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
25 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
26
27 /**
28 * Resizes a given image (if required) and returns its relative path.
29 *
30 * = Examples =
31 *
32 * <code title="Default">
33 * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
34 * </code>
35 * <output>
36 * typo3conf/ext/myext/Resources/Public/typo3_logo.png
37 * or (in BE mode):
38 * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
39 * </output>
40 *
41 * <code title="Image Object">
42 * <f:uri.image image="{imageObject}" />
43 * </code>
44 * <output>
45 * fileadmin/images/image.png
46 * or (in BE mode):
47 * fileadmin/images/image.png
48 * </output>
49 *
50 * <code title="Inline notation">
51 * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png', minWidth: 30, maxWidth: 40)}
52 * </code>
53 * <output>
54 * typo3temp/assets/images/[b4c0e7ed5c].png
55 * (depending on your TYPO3s encryption key)
56 * </output>
57 *
58 * <code title="non existing image">
59 * <f:uri.image src="NonExistingImage.png" />
60 * </code>
61 * <output>
62 * Could not get image resource for "NonExistingImage.png".
63 * </output>
64 */
65 class ImageViewHelper extends AbstractViewHelper
66 {
67 use CompileWithRenderStatic;
68
69 /**
70 * Initialize arguments
71 */
72 public function initializeArguments()
73 {
74 $this->registerArgument('src', 'string', 'src');
75 $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
76 $this->registerArgument('image', 'object', 'image');
77 $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
78 $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
79
80 $this->registerArgument('width', 'string', '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.');
81 $this->registerArgument('height', 'string', '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.');
82 $this->registerArgument('minWidth', 'int', 'minimum width of the image');
83 $this->registerArgument('minHeight', 'int', 'minimum height of the image');
84 $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
85 $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
86 $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
87 }
88
89 /**
90 * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
91 *
92 * @param array $arguments
93 * @param \Closure $renderChildrenClosure
94 * @param RenderingContextInterface $renderingContext
95 * @return string
96 * @throws Exception
97 */
98 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
99 {
100 $src = $arguments['src'];
101 $image = $arguments['image'];
102 $treatIdAsReference = $arguments['treatIdAsReference'];
103 $cropString = $arguments['crop'];
104 $absolute = $arguments['absolute'];
105
106 if (($src === null && $image === null) || ($src !== null && $image !== null)) {
107 throw new Exception('You must either specify a string src or a File object.', 1460976233);
108 }
109
110 try {
111 $imageService = self::getImageService();
112 $image = $imageService->getImage($src, $image, $treatIdAsReference);
113
114 if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
115 $cropString = $image->getProperty('crop');
116 }
117
118 $cropVariantCollection = CropVariantCollection::create((string)$cropString);
119 $cropVariant = $arguments['cropVariant'] ?: 'default';
120 $cropArea = $cropVariantCollection->getCropArea($cropVariant);
121 $processingInstructions = [
122 'width' => $arguments['width'],
123 'height' => $arguments['height'],
124 'minWidth' => $arguments['minWidth'],
125 'minHeight' => $arguments['minHeight'],
126 'maxWidth' => $arguments['maxWidth'],
127 'maxHeight' => $arguments['maxHeight'],
128 'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
129 ];
130
131 $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
132 return $imageService->getImageUri($processedImage, $absolute);
133 } catch (ResourceDoesNotExistException $e) {
134 // thrown if file does not exist
135 throw new Exception($e->getMessage(), 1509741907, $e);
136 } catch (\UnexpectedValueException $e) {
137 // thrown if a file has been replaced with a folder
138 throw new Exception($e->getMessage(), 1509741908, $e);
139 } catch (\RuntimeException $e) {
140 // RuntimeException thrown if a file is outside of a storage
141 throw new Exception($e->getMessage(), 1509741909, $e);
142 } catch (\InvalidArgumentException $e) {
143 // thrown if file storage does not exist
144 throw new Exception($e->getMessage(), 1509741910, $e);
145 }
146 }
147
148 /**
149 * Return an instance of ImageService using object manager
150 *
151 * @return ImageService
152 */
153 protected static function getImageService()
154 {
155 /** @var ObjectManager $objectManager */
156 $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
157 return $objectManager->get(ImageService::class);
158 }
159 }