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