‪TYPO3CMS  11.5
ImageViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
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 
85 class ‪ImageViewHelper extends AbstractViewHelper
86 {
87  use CompileWithRenderStatic;
88 
92  public function ‪initializeArguments()
93  {
94  $this->registerArgument('src', 'string', 'src', false, '');
95  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
96  $this->registerArgument('image', 'object', 'image');
97  $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
98  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
99  $this->registerArgument('fileExtension', 'string', 'Custom file extension to use');
100 
101  $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.');
102  $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.');
103  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
104  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
105  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
106  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
107  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
108  }
109 
119  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
120  {
121  $src = (string)$arguments['src'];
122  $image = $arguments['image'];
123  $treatIdAsReference = (bool)$arguments['treatIdAsReference'];
124  $cropString = $arguments['crop'];
125  $absolute = $arguments['absolute'];
126 
127  if (($src === '' && $image === null) || ($src !== '' && $image !== null)) {
128  throw new Exception('You must either specify a string src or a File object.', 1460976233);
129  }
130 
131  if ((string)$arguments['fileExtension'] && !GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], (string)$arguments['fileExtension'])) {
132  throw new Exception('The extension ' . $arguments['fileExtension'] . ' is not specified in $GLOBALS[\'TYPO3_CONF_VARS\'][\'GFX\'][\'imagefile_ext\'] as a valid image file extension and can not be processed.', 1618992262);
133  }
134 
135  try {
136  $imageService = ‪self::getImageService();
137  $image = $imageService->getImage($src, $image, $treatIdAsReference);
138 
139  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
140  $cropString = $image->getProperty('crop');
141  }
142 
143  // CropVariantCollection needs a string, but this VH could also receive an array
144  if (is_array($cropString)) {
145  $cropString = json_encode($cropString);
146  }
147 
148  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
149  $cropVariant = $arguments['cropVariant'] ?: 'default';
150  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
151  $processingInstructions = [
152  'width' => $arguments['width'],
153  'height' => $arguments['height'],
154  'minWidth' => $arguments['minWidth'],
155  'minHeight' => $arguments['minHeight'],
156  'maxWidth' => $arguments['maxWidth'],
157  'maxHeight' => $arguments['maxHeight'],
158  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
159  ];
160  if (!empty($arguments['fileExtension'])) {
161  $processingInstructions['fileExtension'] = $arguments['fileExtension'];
162  }
163 
164  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
165  return $imageService->getImageUri($processedImage, $absolute);
166  } catch (‪ResourceDoesNotExistException $e) {
167  // thrown if file does not exist
168  throw new Exception($e->getMessage(), 1509741907, $e);
169  } catch (\UnexpectedValueException $e) {
170  // thrown if a file has been replaced with a folder
171  throw new Exception($e->getMessage(), 1509741908, $e);
172  } catch (\RuntimeException $e) {
173  // RuntimeException thrown if a file is outside of a storage
174  throw new Exception($e->getMessage(), 1509741909, $e);
175  } catch (\InvalidArgumentException $e) {
176  // thrown if file storage does not exist
177  throw new Exception($e->getMessage(), 1509741910, $e);
178  }
179  }
180 
186  protected static function ‪getImageService()
187  {
188  return GeneralUtility::makeInstance(ImageService::class);
189  }
190 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
Definition: ImageViewHelper.php:86
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\initializeArguments
‪initializeArguments()
Definition: ImageViewHelper.php:91
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\getImageService
‪static ImageService getImageService()
Definition: ImageViewHelper.php:185
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:16
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ImageViewHelper.php:118
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:42