‪TYPO3CMS  10.4
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', '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  // A URL was given as src, this is kept as is
132  if ($src !== '' && preg_match('/^(https?:)?\/\//', $src)) {
133  return $src;
134  }
135 
136  try {
137  $imageService = ‪self::getImageService();
138  $image = $imageService->getImage($src, $image, $treatIdAsReference);
139 
140  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
141  $cropString = $image->getProperty('crop');
142  }
143 
144  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
145  $cropVariant = $arguments['cropVariant'] ?: 'default';
146  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
147  $processingInstructions = [
148  'width' => $arguments['width'],
149  'height' => $arguments['height'],
150  'minWidth' => $arguments['minWidth'],
151  'minHeight' => $arguments['minHeight'],
152  'maxWidth' => $arguments['maxWidth'],
153  'maxHeight' => $arguments['maxHeight'],
154  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
155  ];
156  if (!empty($arguments['fileExtension'])) {
157  $processingInstructions['fileExtension'] = $arguments['fileExtension'];
158  }
159 
160  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
161  return $imageService->getImageUri($processedImage, $absolute);
162  } catch (‪ResourceDoesNotExistException $e) {
163  // thrown if file does not exist
164  throw new Exception($e->getMessage(), 1509741907, $e);
165  } catch (\UnexpectedValueException $e) {
166  // thrown if a file has been replaced with a folder
167  throw new Exception($e->getMessage(), 1509741908, $e);
168  } catch (\RuntimeException $e) {
169  // RuntimeException thrown if a file is outside of a storage
170  throw new Exception($e->getMessage(), 1509741909, $e);
171  } catch (\InvalidArgumentException $e) {
172  // thrown if file storage does not exist
173  throw new Exception($e->getMessage(), 1509741910, $e);
174  }
175  }
176 
182  protected static function ‪getImageService()
183  {
184  return GeneralUtility::makeInstance(ImageService::class);
185  }
186 }
‪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:35
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\getImageService
‪static ImageService getImageService()
Definition: ImageViewHelper.php:181
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:24
‪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:46
‪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