‪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\ViewHelper\AbstractTagBasedViewHelper;
23 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
24 
91 class ‪ImageViewHelper extends AbstractTagBasedViewHelper
92 {
96  protected ‪$tagName = 'img';
97 
99 
100  public function ‪__construct()
101  {
102  parent::__construct();
103  $this->imageService = GeneralUtility::makeInstance(ImageService::class);
104  }
105 
109  public function ‪initializeArguments()
110  {
111  parent::initializeArguments();
112  $this->registerUniversalTagAttributes();
113  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
114  $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
115  $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
116  $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
117  $this->registerTagAttribute('loading', 'string', 'Native lazy-loading for images property. Can be "lazy", "eager" or "auto"', false);
118  $this->registerTagAttribute('decoding', 'string', 'Provides an image decoding hint to the browser. Can be "sync", "async" or "auto"', false);
119 
120  $this->registerArgument('src', 'string', 'a path to a file, a combined FAL identifier or an uid (int). If $treatIdAsReference is set, the integer is considered the uid of the sys_file_reference record. If you already got a FAL object, consider using the $image parameter instead', false, '');
121  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
122  $this->registerArgument('image', 'object', 'a FAL object (\\TYPO3\\CMS\\Core\\Resource\\File or \\TYPO3\\CMS\\Core\\Resource\\FileReference)');
123  $this->registerArgument('crop', 'string|bool|array', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
124  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
125  $this->registerArgument('fileExtension', 'string', 'Custom file extension to use');
126 
127  $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.');
128  $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.');
129  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
130  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
131  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
132  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
133  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
134  }
135 
144  public function ‪render()
145  {
146  $src = (string)$this->arguments['src'];
147  if (($src === '' && $this->arguments['image'] === null) || ($src !== '' && $this->arguments['image'] !== null)) {
148  throw new Exception('You must either specify a string src or a File object.', 1382284106);
149  }
150 
151  if ((string)$this->arguments['fileExtension'] && !GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], (string)$this->arguments['fileExtension'])) {
152  throw new Exception('The extension ' . $this->arguments['fileExtension'] . ' is not specified in $GLOBALS[\'TYPO3_CONF_VARS\'][\'GFX\'][\'imagefile_ext\'] as a valid image file extension and can not be processed.', 1618989190);
153  }
154 
155  try {
156  $image = $this->imageService->getImage($src, $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
157  $cropString = $this->arguments['crop'];
158  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
159  $cropString = $image->getProperty('crop');
160  }
161 
162  // CropVariantCollection needs a string, but this VH could also receive an array
163  if (is_array($cropString)) {
164  $cropString = json_encode($cropString);
165  }
166 
167  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
168  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
169  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
170  $processingInstructions = [
171  'width' => $this->arguments['width'],
172  'height' => $this->arguments['height'],
173  'minWidth' => $this->arguments['minWidth'],
174  'minHeight' => $this->arguments['minHeight'],
175  'maxWidth' => $this->arguments['maxWidth'],
176  'maxHeight' => $this->arguments['maxHeight'],
177  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
178  ];
179  if (!empty($this->arguments['fileExtension'] ?? '')) {
180  $processingInstructions['fileExtension'] = $this->arguments['fileExtension'];
181  }
182  $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
183  $imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);
184 
185  if (!$this->tag->hasAttribute('data-focus-area')) {
186  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
187  if (!$focusArea->isEmpty()) {
188  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
189  }
190  }
191  $this->tag->addAttribute('src', $imageUri);
192  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
193  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
194 
195  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
196  if (empty($this->arguments['alt'])) {
197  $this->tag->addAttribute('alt', $image->hasProperty('alternative') ? $image->getProperty('alternative') : '');
198  }
199  // Add title-attribute from property if not already set and the property is not an empty string
200  $title = (string)($image->hasProperty('title') ? $image->getProperty('title') : '');
201  if (empty($this->arguments['title']) && $title !== '') {
202  $this->tag->addAttribute('title', $title);
203  }
204  } catch (ResourceDoesNotExistException $e) {
205  // thrown if file does not exist
206  throw new Exception($e->getMessage(), 1509741911, $e);
207  } catch (\UnexpectedValueException $e) {
208  // thrown if a file has been replaced with a folder
209  throw new Exception($e->getMessage(), 1509741912, $e);
210  } catch (\RuntimeException $e) {
211  // RuntimeException thrown if a file is outside of a storage
212  throw new Exception($e->getMessage(), 1509741913, $e);
213  } catch (\InvalidArgumentException $e) {
214  // thrown if file storage does not exist
215  throw new Exception($e->getMessage(), 1509741914, $e);
216  }
217  return $this->tag->render();
218  }
219 }
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\initializeArguments
‪initializeArguments()
Definition: ImageViewHelper.php:108
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
Definition: ImageViewHelper.php:92
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\$tagName
‪string $tagName
Definition: ImageViewHelper.php:95
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\render
‪string render()
Definition: ImageViewHelper.php:143
‪TYPO3\CMS\Fluid\ViewHelpers
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\__construct
‪__construct()
Definition: ImageViewHelper.php:99
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\$imageService
‪ImageService $imageService
Definition: ImageViewHelper.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:42