‪TYPO3CMS  ‪main
ImageViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\RequestInterface;
27 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
28 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
29 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
30 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
31 
106 final class ‪ImageViewHelper extends AbstractViewHelper
107 {
108  use CompileWithRenderStatic;
109 
110  public function ‪initializeArguments(): void
111  {
112  $this->registerArgument('src', 'string', 'src', false, '');
113  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
114  $this->registerArgument('image', 'object', 'image');
115  $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
116  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
117  $this->registerArgument('fileExtension', 'string', 'Custom file extension to use');
118 
119  $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.');
120  $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.');
121  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
122  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
123  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
124  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
125  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
126  }
127 
133  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
134  {
135  $src = (string)$arguments['src'];
136  $image = $arguments['image'];
137  $treatIdAsReference = (bool)$arguments['treatIdAsReference'];
138  $cropString = $arguments['crop'];
139  $absolute = $arguments['absolute'];
140 
141  if (($src === '' && $image === null) || ($src !== '' && $image !== null)) {
142  throw new Exception(self::getExceptionMessage('You must either specify a string src or a File object.', $renderingContext), 1460976233);
143  }
144 
145  if ((string)$arguments['fileExtension'] && !‪GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], (string)$arguments['fileExtension'])) {
146  throw new Exception(
147  self::getExceptionMessage(
148  'The extension ' . $arguments['fileExtension'] . ' is not specified in $GLOBALS[\'TYPO3_CONF_VARS\'][\'GFX\'][\'imagefile_ext\']'
149  . ' as a valid image file extension and can not be processed.',
150  $renderingContext
151  ),
152  1618992262
153  );
154  }
155 
156  try {
157  $imageService = ‪self::getImageService();
158  $image = $imageService->getImage($src, $image, $treatIdAsReference);
159 
160  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
161  $cropString = $image->getProperty('crop');
162  }
163 
164  // CropVariantCollection needs a string, but this VH could also receive an array
165  if (is_array($cropString)) {
166  $cropString = json_encode($cropString);
167  }
168 
169  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
170  $cropVariant = $arguments['cropVariant'] ?: 'default';
171  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
172  $processingInstructions = [
173  'width' => $arguments['width'],
174  'height' => $arguments['height'],
175  'minWidth' => $arguments['minWidth'],
176  'minHeight' => $arguments['minHeight'],
177  'maxWidth' => $arguments['maxWidth'],
178  'maxHeight' => $arguments['maxHeight'],
179  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
180  ];
181  if (!empty($arguments['fileExtension'])) {
182  $processingInstructions['fileExtension'] = $arguments['fileExtension'];
183  }
184 
185  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
186  return $imageService->getImageUri($processedImage, $absolute);
187  } catch (ResourceDoesNotExistException $e) {
188  // thrown if file does not exist
189  throw new Exception(self::getExceptionMessage($e->getMessage(), $renderingContext), 1509741907, $e);
190  } catch (\UnexpectedValueException $e) {
191  // thrown if a file has been replaced with a folder
192  throw new Exception(self::getExceptionMessage($e->getMessage(), $renderingContext), 1509741908, $e);
193  } catch (\InvalidArgumentException $e) {
194  // thrown if file storage does not exist
195  throw new Exception(self::getExceptionMessage($e->getMessage(), $renderingContext), 1509741910, $e);
196  }
197  }
198 
199  protected static function ‪getExceptionMessage(string $detailedMessage, RenderingContextInterface $renderingContext): string
200  {
202  $request = $renderingContext->getRequest();
203  if ($request instanceof ‪RequestInterface) {
204  $currentContentObject = $request->getAttribute('currentContentObject');
205  if ($currentContentObject instanceof ‪ContentObjectRenderer) {
206  return sprintf('Unable to render image uri in "%s": %s', $currentContentObject->currentRecord, $detailedMessage);
207  }
208  }
209  return sprintf('Unable to render image uri: %s', $detailedMessage);
210  }
211 
212  protected static function ‪getImageService(): ‪ImageService
213  {
214  return GeneralUtility::makeInstance(ImageService::class);
215  }
216 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
Definition: ImageViewHelper.php:107
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\initializeArguments
‪initializeArguments()
Definition: ImageViewHelper.php:109
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\renderStatic
‪static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ImageViewHelper.php:132
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:38
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪TYPO3\CMS\Extbase\Mvc\RequestInterface
Definition: RequestInterface.php:24
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:103
‪TYPO3\CMS\Core\Utility\GeneralUtility\inList
‪static bool inList($list, $item)
Definition: GeneralUtility.php:422
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContext
Definition: RenderingContext.php:35
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\getImageService
‪static getImageService()
Definition: ImageViewHelper.php:211
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\getExceptionMessage
‪static getExceptionMessage(string $detailedMessage, RenderingContextInterface $renderingContext)
Definition: ImageViewHelper.php:198