1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Override the ViewHelper with ratio. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace HDNET\Focuspoint\ViewHelpers; |
8
|
|
|
|
9
|
|
|
use HDNET\Focuspoint\Service\FocusCropService; |
10
|
|
|
use TYPO3\CMS\Core\Resource\FileInterface; |
11
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Override the ViewHelper with ratio. |
15
|
|
|
*/ |
16
|
|
|
class ImageViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\ImageViewHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Initialize ViewHelper arguments. |
20
|
|
|
*/ |
21
|
|
|
public function initializeArguments() |
22
|
|
|
{ |
23
|
|
|
parent::initializeArguments(); |
24
|
|
|
$this->registerArgument('ratio', 'string', 'Ratio of the image', false, '1:1'); |
25
|
|
|
$this->registerArgument('realCrop', 'boolean', 'Crop the image in real', false, true); |
26
|
|
|
$this->registerArgument('additionalClassDiv', 'string', 'Additional class for focus point div', false, ''); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Resize a given image (if required) and renders the respective img tag. |
31
|
|
|
* |
32
|
|
|
* @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception |
33
|
|
|
* |
34
|
|
|
* @return string Rendered tag |
35
|
|
|
*/ |
36
|
|
|
public function render() |
37
|
|
|
{ |
38
|
|
|
/** @var FocusCropService $service */ |
39
|
|
|
$service = GeneralUtility::makeInstance(FocusCropService::class); |
40
|
|
|
$internalImage = null; |
41
|
|
|
try { |
42
|
|
|
$internalImage = $service->getViewHelperImage($this->arguments['src'], $this->arguments['image'], $this->arguments['treatIdAsReference']); |
43
|
|
|
if ($this->arguments['realCrop'] && $internalImage instanceof FileInterface) { |
44
|
|
|
$this->arguments['src'] = $service->getCroppedImageSrcByFile($internalImage, $this->arguments['ratio']); |
45
|
|
|
$this->arguments['treatIdAsReference'] = false; |
46
|
|
|
$this->arguments['image'] = null; |
47
|
|
|
} |
48
|
|
|
} catch (\Exception $ex) { |
49
|
|
|
$this->arguments['realCrop'] = true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
parent::render(); |
54
|
|
|
} catch (\Exception $ex) { |
55
|
|
|
return 'Missing image!'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->arguments['realCrop']) { |
59
|
|
|
return $this->tag->render(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Ratio calculation |
63
|
|
|
if (null !== $internalImage) { |
64
|
|
|
$focusPointY = $internalImage->getProperty('focus_point_y'); |
|
|
|
|
65
|
|
|
$focusPointX = $internalImage->getProperty('focus_point_x'); |
66
|
|
|
|
67
|
|
|
$additionalClassDiv = 'focuspoint'; |
68
|
|
|
if (!empty($this->arguments['additionalClassDiv'])) { |
69
|
|
|
$additionalClassDiv .= ' ' . $this->arguments['additionalClassDiv']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$focusTag = '<div class="' . $additionalClassDiv . '" data-image-imageSrc="' . $this->tag->getAttribute('src') . '" data-focus-x="' . ($focusPointX / 100) . '" data-focus-y="' . ($focusPointY / 100) . '" data-image-w="' . $this->tag->getAttribute('width') . '" data-image-h="' . $this->tag->getAttribute('height') . '">'; |
73
|
|
|
|
74
|
|
|
return $focusTag . $this->tag->render() . '</div>'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return 'Missing internal image!'; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: