TYPO3 CMS  TYPO3_8-7
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 
26 
66 {
68 
72  public function initializeArguments()
73  {
74  parent::initializeArguments();
75  $this->registerArgument('src', 'string', 'src');
76  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
77  $this->registerArgument('image', 'object', 'image');
78  $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
79  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
80 
81  $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.');
82  $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.');
83  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
84  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
85  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
86  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
87  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
88  }
89 
99  public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
100  {
101  $src = $arguments['src'];
102  $image = $arguments['image'];
103  $treatIdAsReference = $arguments['treatIdAsReference'];
104  $cropString = $arguments['crop'];
105  $absolute = $arguments['absolute'];
106 
107  if ((is_null($src) && is_null($image)) || (!is_null($src) && !is_null($image))) {
108  throw new Exception('You must either specify a string src or a File object.', 1460976233);
109  }
110 
111  try {
112  $imageService = self::getImageService();
113  $image = $imageService->getImage($src, $image, $treatIdAsReference);
114 
115  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
116  $cropString = $image->getProperty('crop');
117  }
118 
119  $cropVariantCollection = CropVariantCollection::create((string)$cropString);
120  $cropVariant = $arguments['cropVariant'] ?: 'default';
121  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
122  $processingInstructions = [
123  'width' => $arguments['width'],
124  'height' => $arguments['height'],
125  'minWidth' => $arguments['minWidth'],
126  'minHeight' => $arguments['minHeight'],
127  'maxWidth' => $arguments['maxWidth'],
128  'maxHeight' => $arguments['maxHeight'],
129  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
130  ];
131 
132  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
133  return $imageService->getImageUri($processedImage, $absolute);
134  } catch (ResourceDoesNotExistException $e) {
135  // thrown if file does not exist
136  } catch (\UnexpectedValueException $e) {
137  // thrown if a file has been replaced with a folder
138  } catch (\RuntimeException $e) {
139  // RuntimeException thrown if a file is outside of a storage
140  } catch (\InvalidArgumentException $e) {
141  // thrown if file storage does not exist
142  }
143  return '';
144  }
145 
151  protected static function getImageService()
152  {
154  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
155  return $objectManager->get(ImageService::class);
156  }
157 }
static makeInstance($className,... $constructorArguments)
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
static create(string $jsonString, array $tcaConfig=[])