‪TYPO3CMS  11.5
ThumbnailViewHelper.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\Exception;
23 
61 {
65  public function ‪initializeArguments()
66  {
67  parent::initializeArguments();
68  $this->registerArgument('context', 'string', 'context for image rendering', false, ‪ProcessedFile::CONTEXT_IMAGEPREVIEW);
69  }
70 
79  public function ‪render()
80  {
81  if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) {
82  throw new Exception('You must either specify a string src or a File object.', 1533290762);
83  }
84 
85  try {
86  $image = $this->imageService->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
87 
88  $cropString = $this->arguments['crop'];
89  if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
90  $cropString = $image->getProperty('crop');
91  }
92  $cropVariantCollection = ‪CropVariantCollection::create((string)$cropString);
93  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
94  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
95  $processingInstructions = [];
96  if (!$cropArea->isEmpty()) {
97  $processingInstructions['crop'] = $cropArea->makeAbsoluteBasedOnFile($image);
98  }
99  foreach (['width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight'] as $argument) {
100  if (!empty($this->arguments[$argument])) {
101  $processingInstructions[$argument] = $this->arguments[$argument];
102  }
103  }
104 
105  $processedFile = $image->process($this->arguments['context'], $processingInstructions);
106  $imageUri = $processedFile->getPublicUrl();
107 
108  if (!$this->tag->hasAttribute('data-focus-area')) {
109  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
110  if (!$focusArea->isEmpty()) {
111  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
112  }
113  }
114  $this->tag->addAttribute('src', $imageUri);
115  $this->tag->addAttribute('width', $processedFile->getProperty('width'));
116  $this->tag->addAttribute('height', $processedFile->getProperty('height'));
117 
118  $alt = $image->getProperty('alternative');
119  $title = $image->getProperty('title');
120 
121  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
122  if (empty($this->arguments['alt'])) {
123  $this->tag->addAttribute('alt', $alt);
124  }
125  if (empty($this->arguments['title']) && $title) {
126  $this->tag->addAttribute('title', $title);
127  }
128  } catch (‪ResourceDoesNotExistException $e) {
129  // thrown if file does not exist
130  throw new Exception($e->getMessage(), 1533294109, $e);
131  } catch (\UnexpectedValueException $e) {
132  // thrown if a file has been replaced with a folder
133  throw new Exception($e->getMessage(), 1533294113, $e);
134  } catch (\RuntimeException $e) {
135  // RuntimeException thrown if a file is outside of a storage
136  throw new Exception($e->getMessage(), 1533294116, $e);
137  } catch (\InvalidArgumentException $e) {
138  // thrown if file storage does not exist
139  throw new Exception($e->getMessage(), 1533294120, $e);
140  }
141 
142  return $this->tag->render();
143  }
144 }
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\render
‪string render()
Definition: ThumbnailViewHelper.php:79
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:53
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper
Definition: ThumbnailViewHelper.php:61
‪TYPO3\CMS\Backend\ViewHelpers\ThumbnailViewHelper\initializeArguments
‪initializeArguments()
Definition: ThumbnailViewHelper.php:65
‪TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper
Definition: ImageViewHelper.php:92
‪TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException
Definition: ResourceDoesNotExistException.php:23
‪TYPO3\CMS\Backend\ViewHelpers
Definition: ArrayBrowserViewHelper.php:18
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:42