‪TYPO3CMS  ‪main
ThumbnailViewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
26 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
27 
64 final class ‪ThumbnailViewHelper extends AbstractTagBasedViewHelper
65 {
69  protected ‪$tagName = 'img';
70 
72 
73  public function ‪__construct()
74  {
75  parent::__construct();
76  $this->imageService = GeneralUtility::makeInstance(ImageService::class);
77  }
78 
79  public function ‪initializeArguments(): void
80  {
81  parent::initializeArguments();
82  $this->registerUniversalTagAttributes();
83  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
84 
85  $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, '');
86  $this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
87  $this->registerArgument('image', 'object', 'a FAL object (\\TYPO3\\CMS\\Core\\Resource\\File or \\TYPO3\\CMS\\Core\\Resource\\FileReference)');
88  $this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
89  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
90 
91  $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.');
92  $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.');
93  $this->registerArgument('minWidth', 'int', 'minimum width of the image');
94  $this->registerArgument('minHeight', 'int', 'minimum height of the image');
95  $this->registerArgument('maxWidth', 'int', 'maximum width of the image');
96  $this->registerArgument('maxHeight', 'int', 'maximum height of the image');
97  $this->registerArgument('context', 'string', 'context for image rendering', false, ‪ProcessedFile::CONTEXT_IMAGEPREVIEW);
98  }
99 
103  public function ‪render(): string
104  {
105  if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) {
106  throw new Exception('You must either specify a string src or a File object.', 1533290762);
107  }
108 
109  try {
110  $image = $this->imageService->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
111 
112  $cropString = $this->arguments['crop'];
113  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
114  $cropString = $image->getProperty('crop');
115  }
116  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
117  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
118  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
119  $processingInstructions = [];
120  if (!$cropArea->isEmpty()) {
121  $processingInstructions['crop'] = $cropArea->makeAbsoluteBasedOnFile($image);
122  }
123  foreach (['width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight'] as $argument) {
124  if (!empty($this->arguments[$argument])) {
125  $processingInstructions[$argument] = $this->arguments[$argument];
126  }
127  }
128 
129  if (is_callable([$image, 'getOriginalFile'])) {
130  // Get the original file from the file reference
131  $image = $image->getOriginalFile();
132  }
133 
134  $processedFile = $image->process($this->arguments['context'], $processingInstructions);
135  $imageUri = $processedFile->getPublicUrl();
136 
137  if (!$this->tag->hasAttribute('data-focus-area')) {
138  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
139  if (!$focusArea->isEmpty()) {
140  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
141  }
142  }
143  $this->tag->addAttribute('src', $imageUri);
144  $this->tag->addAttribute('width', $processedFile->getProperty('width'));
145  $this->tag->addAttribute('height', $processedFile->getProperty('height'));
146 
147  $alt = $image->getProperty('alternative');
148  $title = $image->getProperty('title');
149 
150  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
151  if (empty($this->arguments['alt'])) {
152  $this->tag->addAttribute('alt', $alt);
153  }
154  if (empty($this->arguments['title']) && $title) {
155  $this->tag->addAttribute('title', $title);
156  }
157  } catch (ResourceDoesNotExistException $e) {
158  // thrown if file does not exist
159  throw new Exception($e->getMessage(), 1533294109, $e);
160  } catch (\UnexpectedValueException $e) {
161  // thrown if a file has been replaced with a folder
162  throw new Exception($e->getMessage(), 1533294113, $e);
163  } catch (\RuntimeException $e) {
164  // RuntimeException thrown if a file is outside of a storage
165  throw new Exception($e->getMessage(), 1533294116, $e);
166  } catch (\InvalidArgumentException $e) {
167  // thrown if file storage does not exist
168  throw new Exception($e->getMessage(), 1533294120, $e);
169  }
170 
171  return $this->tag->render();
172  }
173 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:55
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper
Definition: ThumbnailViewHelper.php:65
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\initializeArguments
‪initializeArguments()
Definition: ThumbnailViewHelper.php:78
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\$imageService
‪ImageService $imageService
Definition: ThumbnailViewHelper.php:70
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:38
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\render
‪render()
Definition: ThumbnailViewHelper.php:102
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪TYPO3\CMS\Backend\ViewHelpers
Definition: AvatarViewHelper.php:18
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\__construct
‪__construct()
Definition: ThumbnailViewHelper.php:72
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\$tagName
‪string $tagName
Definition: ThumbnailViewHelper.php:68