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
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Backend\ViewHelpers; |
17
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection; |
19
|
|
|
use TYPO3\CMS\Core\Resource\Exception\ResourceDoesNotExistException; |
20
|
|
|
use TYPO3\CMS\Core\Resource\ProcessedFile; |
21
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
22
|
|
|
use TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper; |
23
|
|
|
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ViewHelper for the backend which generates an :html:`<img>` tag with the special URI to render thumbnails deferred. |
27
|
|
|
* |
28
|
|
|
* Examples |
29
|
|
|
* ======== |
30
|
|
|
* |
31
|
|
|
* Default |
32
|
|
|
* ------- |
33
|
|
|
* |
34
|
|
|
* :: |
35
|
|
|
* |
36
|
|
|
* <be:thumbnail image="{file.resource}" width="{thumbnail.width}" height="{thumbnail.height}" /> |
37
|
|
|
* |
38
|
|
|
* Output:: |
39
|
|
|
* |
40
|
|
|
* <img src="/typo3/thumbnails?token=¶meters={"fileId":1,"configuration":{"_context":"Image.Preview","maxWidth":64,"maxHeight":64}}&hmac=" |
41
|
|
|
* width="64" |
42
|
|
|
* height="64" |
43
|
|
|
* alt="alt set in image record" |
44
|
|
|
* title="title set in image record"/> |
45
|
|
|
* |
46
|
|
|
* Inline notation |
47
|
|
|
* --------------- |
48
|
|
|
* |
49
|
|
|
* :: |
50
|
|
|
* |
51
|
|
|
* {be:thumbnail(image: file.resource, maxWidth: thumbnail.width, maxHeight: thumbnail.height)} |
52
|
|
|
* |
53
|
|
|
* Output:: |
54
|
|
|
* |
55
|
|
|
* <img src="/typo3/thumbnails?token=¶meters={"fileId":1,"configuration":{"_context":"Image.Preview","maxWidth":64,"maxHeight":64}}&hmac=" |
56
|
|
|
* width="64" |
57
|
|
|
* height="64" |
58
|
|
|
* alt="alt set in image record" |
59
|
|
|
* title="title set in image record"/> |
60
|
|
|
*/ |
61
|
|
|
class ThumbnailViewHelper extends ImageViewHelper |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Initialize arguments. |
66
|
|
|
*/ |
67
|
|
|
public function initializeArguments() |
68
|
|
|
{ |
69
|
|
|
parent::initializeArguments(); |
70
|
|
|
$this->registerArgument('context', 'string', 'context for image rendering', false, ProcessedFile::CONTEXT_IMAGEPREVIEW); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Resizes a given image (if required) and renders the respective img tag |
75
|
|
|
* |
76
|
|
|
* @see https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/ |
77
|
|
|
* |
78
|
|
|
* @throws Exception |
79
|
|
|
* @return string Rendered tag |
80
|
|
|
*/ |
81
|
|
|
public function render() |
82
|
|
|
{ |
83
|
|
|
if (($this->arguments['src'] === '' && $this->arguments['image'] === null) || ($this->arguments['src'] !== '' && $this->arguments['image'] !== null)) { |
84
|
|
|
throw new Exception('You must either specify a string src or a File object.', 1533290762); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$image = $this->getImageService()->getImage((string)$this->arguments['src'], $this->arguments['image'], (bool)$this->arguments['treatIdAsReference']); |
89
|
|
|
|
90
|
|
|
$cropString = $this->arguments['crop']; |
91
|
|
|
if ($cropString === null && $image->hasProperty('crop') && $image->getProperty('crop')) { |
92
|
|
|
$cropString = $image->getProperty('crop'); |
93
|
|
|
} |
94
|
|
|
$cropVariantCollection = CropVariantCollection::create((string)$cropString); |
95
|
|
|
$cropVariant = $this->arguments['cropVariant'] ?: 'default'; |
96
|
|
|
$cropArea = $cropVariantCollection->getCropArea($cropVariant); |
97
|
|
|
$processingInstructions = []; |
98
|
|
|
if (!$cropArea->isEmpty()) { |
99
|
|
|
$processingInstructions['crop'] = $cropArea->makeAbsoluteBasedOnFile($image); |
100
|
|
|
} |
101
|
|
|
foreach (['width', 'height', 'minWidth', 'minHeight', 'maxWidth', 'maxHeight'] as $argument) { |
102
|
|
|
if (!empty($this->arguments[$argument])) { |
103
|
|
|
$processingInstructions[$argument] = $this->arguments[$argument]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$processedFile = $image->process($this->arguments['context'], $processingInstructions); |
|
|
|
|
108
|
|
|
$imageUri = PathUtility::getAbsoluteWebPath($processedFile->getPublicUrl()); |
109
|
|
|
|
110
|
|
|
if (!$this->tag->hasAttribute('data-focus-area')) { |
111
|
|
|
$focusArea = $cropVariantCollection->getFocusArea($cropVariant); |
112
|
|
|
if (!$focusArea->isEmpty()) { |
113
|
|
|
$this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
$this->tag->addAttribute('src', $imageUri); |
117
|
|
|
$this->tag->addAttribute('width', $processedFile->getProperty('width')); |
118
|
|
|
$this->tag->addAttribute('height', $processedFile->getProperty('height')); |
119
|
|
|
|
120
|
|
|
$alt = $image->getProperty('alternative'); |
121
|
|
|
$title = $image->getProperty('title'); |
122
|
|
|
|
123
|
|
|
// The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty |
124
|
|
|
if (empty($this->arguments['alt'])) { |
125
|
|
|
$this->tag->addAttribute('alt', $alt); |
126
|
|
|
} |
127
|
|
|
if (empty($this->arguments['title']) && $title) { |
128
|
|
|
$this->tag->addAttribute('title', $title); |
129
|
|
|
} |
130
|
|
|
} catch (ResourceDoesNotExistException $e) { |
131
|
|
|
// thrown if file does not exist |
132
|
|
|
throw new Exception($e->getMessage(), 1533294109, $e); |
133
|
|
|
} catch (\UnexpectedValueException $e) { |
134
|
|
|
// thrown if a file has been replaced with a folder |
135
|
|
|
throw new Exception($e->getMessage(), 1533294113, $e); |
136
|
|
|
} catch (\RuntimeException $e) { |
137
|
|
|
// RuntimeException thrown if a file is outside of a storage |
138
|
|
|
throw new Exception($e->getMessage(), 1533294116, $e); |
139
|
|
|
} catch (\InvalidArgumentException $e) { |
140
|
|
|
// thrown if file storage does not exist |
141
|
|
|
throw new Exception($e->getMessage(), 1533294120, $e); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->tag->render(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.