-
Notifications
You must be signed in to change notification settings - Fork 638
/
ThumbnailViewHelper.php
173 lines (154 loc) · 7.73 KB
/
ThumbnailViewHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Backend\ViewHelpers;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException;
use TYPO3\CMS\Core\Resource\ProcessedFile;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Service\ImageService;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
/**
* ViewHelper for the backend which generates an :html:`<img>` tag with the special URI to render thumbnails deferred.
*
* Examples
* ========
*
* Default
* -------
*
* ::
*
* <be:thumbnail image="{file.resource}" width="{thumbnail.width}" height="{thumbnail.height}" />
*
* Output::
*
* <img src="https://www.example.com/typo3/image/process?token=XYZ&id=1160"
* width="64"
* height="64"
* alt="alt set in image record"
* title="title set in image record"/>
*
* Inline notation
* ---------------
*
* ::
*
* {be:thumbnail(image: file.resource, maxWidth: thumbnail.width, maxHeight: thumbnail.height)}
*
* Output::
*
* <img src="https://www.example.com/typo3/image/process?token=XYZ&id=1160"
* width="64"
* height="64"
* alt="alt set in image record"
* title="title set in image record"/>
*/
final class ThumbnailViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var string
*/
protected $tagName = 'img';
protected ImageService $imageService;
public function __construct()
{
parent::__construct();
$this->imageService = GeneralUtility::makeInstance(ImageService::class);
}
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
$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, '');
$this->registerArgument('treatIdAsReference', 'bool', 'given src argument is a sys_file_reference record', false, false);
$this->registerArgument('image', 'object', 'a FAL object (\\TYPO3\\CMS\\Core\\Resource\\File or \\TYPO3\\CMS\\Core\\Resource\\FileReference)');
$this->registerArgument('crop', 'string|bool', 'overrule cropping of image (setting to FALSE disables the cropping set in FileReference)');
$this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
$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.');
$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.');
$this->registerArgument('minWidth', 'int', 'minimum width of the image');
$this->registerArgument('minHeight', 'int', 'minimum height of the image');
$this->registerArgument('maxWidth', 'int', 'maximum width of the image');
$this->registerArgument('maxHeight', 'int', 'maximum height of the image');
$this->registerArgument('context', 'string', 'context for image rendering', false, ProcessedFile::CONTEXT_IMAGEPREVIEW);
}
/**
* @throws Exception
*/
public function render(): string
{
if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) {
throw new Exception('You must either specify a string src or a File object.', 1533290762);
}
try {
$image = $this->imageService->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']);
$cropString = $this->arguments['crop'];
if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) {
$cropString = $image->getProperty('crop');
}
$cropVariantCollection = CropVariantCollection::create((string)$cropString);
$cropVariant = $this->arguments['cropVariant'] ?: 'default';
$cropArea = $cropVariantCollection->getCropArea($cropVariant);
$processingInstructions = [];
if (!$cropArea->isEmpty()) {
$processingInstructions['crop'] = $cropArea->makeAbsoluteBasedOnFile($image);
}
foreach (['width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight'] as $argument) {
if (!empty($this->arguments[$argument])) {
$processingInstructions[$argument] = $this->arguments[$argument];
}
}
if (is_callable([$image, 'getOriginalFile'])) {
// Get the original file from the file reference
$image = $image->getOriginalFile();
}
$processedFile = $image->process($this->arguments['context'], $processingInstructions);
$imageUri = $processedFile->getPublicUrl();
if (!$this->tag->hasAttribute('data-focus-area')) {
$focusArea = $cropVariantCollection->getFocusArea($cropVariant);
if (!$focusArea->isEmpty()) {
$this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
}
}
$this->tag->addAttribute('src', $imageUri);
$this->tag->addAttribute('width', $processedFile->getProperty('width'));
$this->tag->addAttribute('height', $processedFile->getProperty('height'));
$alt = $image->getProperty('alternative');
$title = $image->getProperty('title');
// The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
if (empty($this->arguments['alt'])) {
$this->tag->addAttribute('alt', $alt);
}
if (empty($this->arguments['title']) && $title) {
$this->tag->addAttribute('title', $title);
}
} catch (ResourceDoesNotExistException $e) {
// thrown if file does not exist
throw new Exception($e->getMessage(), 1533294109, $e);
} catch (\UnexpectedValueException $e) {
// thrown if a file has been replaced with a folder
throw new Exception($e->getMessage(), 1533294113, $e);
} catch (\RuntimeException $e) {
// RuntimeException thrown if a file is outside of a storage
throw new Exception($e->getMessage(), 1533294116, $e);
} catch (\InvalidArgumentException $e) {
// thrown if file storage does not exist
throw new Exception($e->getMessage(), 1533294120, $e);
}
return $this->tag->render();
}
}