‪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 
19 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21 
86 class ‪ImageViewHelper extends AbstractTagBasedViewHelper
87 {
91  protected ‪$tagName = 'img';
92 
96  protected ‪$imageService;
97 
102  {
103  $this->imageService = ‪$imageService;
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 
118  $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');
119  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record');
120  $this->registerArgument('image', 'object', 'a FAL object');
121  $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
122  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
123 
124  $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.');
125  $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.');
126  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
127  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
128  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
129  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
130  $this->registerArgument('absolute', 'bool', 'Force absolute URL', false, false);
131  }
132 
141  public function ‪render()
142  {
143  if (($this->arguments['src'] === null && $this->arguments['image'] === null) || ($this->arguments['src'] !== null && $this->arguments['image'] !== null)) {
144  throw new Exception('You must either specify a string src or a File object.', 1382284106);
145  }
146 
147  try {
148  $image = $this->imageService->getImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']);
149  $cropString = $this->arguments['crop'];
150  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
151  $cropString = $image->getProperty('crop');
152  }
153  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
154  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
155  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
156  $processingInstructions = [
157  'width' => $this->arguments['width'],
158  'height' => $this->arguments['height'],
159  'minWidth' => $this->arguments['minWidth'],
160  'minHeight' => $this->arguments['minHeight'],
161  'maxWidth' => $this->arguments['maxWidth'],
162  'maxHeight' => $this->arguments['maxHeight'],
163  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
164  ];
165  $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
166  $imageUri = $this->imageService->getImageUri($processedImage, $this->arguments['absolute']);
167 
168  if (!$this->tag->hasAttribute('data-focus-area')) {
169  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
170  if (!$focusArea->isEmpty()) {
171  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
172  }
173  }
174  $this->tag->addAttribute('src', $imageUri);
175  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
176  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
177 
178  $alt = $image->getProperty('alternative');
179  $title = $image->getProperty('title');
180 
181  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
182  if (empty($this->arguments['alt'])) {
183  $this->tag->addAttribute('alt', $alt);
184  }
185  if (empty($this->arguments['title']) && $title) {
186  $this->tag->addAttribute('title', $title);
187  }
188  } catch (‪ResourceDoesNotExistException $e) {
189  // thrown if file does not exist
190  throw new Exception($e->getMessage(), 1509741911, $e);
191  } catch (\UnexpectedValueException $e) {
192  // thrown if a file has been replaced with a folder
193  throw new Exception($e->getMessage(), 1509741912, $e);
194  } catch (\RuntimeException $e) {
195  // RuntimeException thrown if a file is outside of a storage
196  throw new Exception($e->getMessage(), 1509741913, $e);
197  } catch (\InvalidArgumentException $e) {
198  // thrown if file storage does not exist
199  throw new Exception($e->getMessage(), 1509741914, $e);
200  }
201 
202  return $this->tag->render();
203  }
204 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\$imageService
‪TYPO3 CMS Extbase Service ImageService $imageService
Definition: ImageViewHelper.php:94
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\injectImageService
‪injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
Definition: ImageViewHelper.php:99
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\initializeArguments
‪initializeArguments()
Definition: ImageViewHelper.php:107
‪TYPO3
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
Definition: ImageViewHelper.php:87
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:30
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:21
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\$tagName
‪string $tagName
Definition: ImageViewHelper.php:90
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper\render
‪string render()
Definition: ImageViewHelper.php:139
‪TYPO3\CMS\Fluid\ViewHelpers
Definition: BaseViewHelper.php:2
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:40