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