‪TYPO3CMS  9.5
ImageViewHelper.php
Go to the documentation of this file.
1 <?php
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 
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 
83 class ‪ImageViewHelper extends AbstractViewHelper
84 {
85  use CompileWithRenderStatic;
86 
90  public function ‪initializeArguments()
91  {
92  $this->registerArgument('src', 'string', 'src');
93  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
94  $this->registerArgument('image', 'object', 'image');
95  $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
96  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
97 
98  $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.');
99  $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.');
100  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
101  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
102  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
103  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
104  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
105  }
106 
116  public static function ‪renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
117  {
118  $src = $arguments['src'];
119  $image = $arguments['image'];
120  $treatIdAsReference = $arguments['treatIdAsReference'];
121  $cropString = $arguments['crop'];
122  $absolute = $arguments['absolute'];
123 
124  if (($src === null && $image === null) || ($src !== null && $image !== null)) {
125  throw new Exception('You must either specify a string src or a File object.', 1460976233);
126  }
127 
128  try {
129  $imageService = ‪self::getImageService();
130  $image = $imageService->getImage($src, $image, $treatIdAsReference);
131 
132  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
133  $cropString = $image->getProperty('crop');
134  }
135 
136  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
137  $cropVariant = $arguments['cropVariant'] ?: 'default';
138  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
139  $processingInstructions = [
140  'width' => $arguments['width'],
141  'height' => $arguments['height'],
142  'minWidth' => $arguments['minWidth'],
143  'minHeight' => $arguments['minHeight'],
144  'maxWidth' => $arguments['maxWidth'],
145  'maxHeight' => $arguments['maxHeight'],
146  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
147  ];
148 
149  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
150  return $imageService->getImageUri($processedImage, $absolute);
151  } catch (‪ResourceDoesNotExistException $e) {
152  // thrown if file does not exist
153  throw new Exception($e->getMessage(), 1509741907, $e);
154  } catch (\UnexpectedValueException $e) {
155  // thrown if a file has been replaced with a folder
156  throw new Exception($e->getMessage(), 1509741908, $e);
157  } catch (\RuntimeException $e) {
158  // RuntimeException thrown if a file is outside of a storage
159  throw new Exception($e->getMessage(), 1509741909, $e);
160  } catch (\InvalidArgumentException $e) {
161  // thrown if file storage does not exist
162  throw new Exception($e->getMessage(), 1509741910, $e);
163  }
164  }
165 
171  protected static function ‪getImageService()
172  {
174  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
175  return $objectManager->get(ImageService::class);
176  }
177 }
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper
Definition: ImageViewHelper.php:84
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\initializeArguments
‪initializeArguments()
Definition: ImageViewHelper.php:89
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:30
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\getImageService
‪static ImageService getImageService()
Definition: ImageViewHelper.php:170
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:21
‪TYPO3\CMS\Fluid\ViewHelpers\Uri
Definition: ActionViewHelper.php:2
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Fluid\ViewHelpers\Uri\ImageViewHelper\renderStatic
‪static string renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
Definition: ImageViewHelper.php:115
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:40
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:25