forked from neos/neos-development-collection
/
ImageViewHelper.php
139 lines (129 loc) · 5.45 KB
/
ImageViewHelper.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
<?php
namespace TYPO3\Media\ViewHelpers;
/*
* This file is part of the TYPO3.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
use TYPO3\Media\Domain\Model\AssetInterface;
use TYPO3\Media\Domain\Model\ImageInterface;
use TYPO3\Media\Domain\Model\ThumbnailConfiguration;
use TYPO3\Media\Domain\Model\ThumbnailSupportInterface;
/**
* Renders an <img> HTML tag from a given TYPO3.Media's asset instance
*
* = Examples =
*
* <code title="Rendering an image as-is">
* <typo3.media:image image="{imageObject}" alt="a sample image without scaling" />
* </code>
* <output>
* (depending on the image, no scaling applied)
* <img src="_Resources/Persistent/b29[...]95d.jpeg" width="120" height="180" alt="a sample image without scaling" />
* </output>
*
*
* <code title="Rendering an image with scaling at a given width only">
* <typo3.media:image image="{imageObject}" maximumWidth="80" alt="sample" />
* </code>
* <output>
* (depending on the image; scaled down to a maximum width of 80 pixels, keeping the aspect ratio)
* <img src="_Resources/Persistent/b29[...]95d.jpeg" width="80" height="120" alt="sample" />
* </output>
*
*
* <code title="Rendering an image with scaling at given width and height, keeping aspect ratio">
* <typo3.media:image image="{imageObject}" maximumWidth="80" maximumHeight="80" alt="sample" />
* </code>
* <output>
* (depending on the image; scaled down to a maximum width and height of 80 pixels, keeping the aspect ratio)
* <img src="_Resources/Persistent/b29[...]95d.jpeg" width="53" height="80" alt="sample" />
* </output>
*
*
* <code title="Rendering an image with crop-scaling at given width and height">
* <typo3.media:image image="{imageObject}" maximumWidth="80" maximumHeight="80" allowCropping="true" alt="sample" />
* </code>
* <output>
* (depending on the image; scaled down to a width and height of 80 pixels, possibly changing aspect ratio)
* <img src="_Resources/Persistent/b29[...]95d.jpeg" width="80" height="80" alt="sample" />
* </output>
*
* <code title="Rendering an image with allowed up-scaling at given width and height">
* <typo3.media:image image="{imageObject}" maximumWidth="5000" allowUpScaling="true" alt="sample" />
* </code>
* <output>
* (depending on the image; scaled up or down to a width 5000 pixels, keeping aspect ratio)
* <img src="_Resources/Persistent/b29[...]95d.jpeg" width="80" height="80" alt="sample" />
* </output>
*
*/
class ImageViewHelper extends AbstractTagBasedViewHelper
{
/**
* @var \TYPO3\Flow\Resource\ResourceManager
* @Flow\Inject
*/
protected $resourceManager;
/**
* @Flow\Inject
* @var \TYPO3\Media\Domain\Service\ThumbnailService
*/
protected $thumbnailService;
/**
* @Flow\Inject
* @var \TYPO3\Media\Domain\Service\AssetService
*/
protected $assetService;
/**
* name of the tag to be created by this view helper
*
* @var string
*/
protected $tagName = 'img';
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerUniversalTagAttributes();
$this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', true);
$this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
$this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
// @deprecated since 2.0 use the "image" argument instead
$this->registerArgument('asset', 'TYPO3\Media\Domain\Model\AssetInterface', 'The image to be rendered - DEPRECATED, use "image" argument instead', false);
}
/**
* Renders an HTML img tag with a thumbnail image, created from a given asset.
*
* @param ImageInterface $image The image to be rendered as an image
* @param integer $width Desired width of the image
* @param integer $maximumWidth Desired maximum width of the image
* @param integer $height Desired height of the image
* @param integer $maximumHeight Desired maximum height of the image
* @param boolean $allowCropping Whether the image should be cropped if the given sizes would hurt the aspect ratio
* @param boolean $allowUpScaling Whether the resulting image size might exceed the size of the original image
* @return string an <img...> html tag
*/
public function render(ImageInterface $image = null, $width = null, $maximumWidth = null, $height = null, $maximumHeight = null, $allowCropping = false, $allowUpScaling = false)
{
if ($image === null && $this->hasArgument('asset')) {
$image = $this->arguments['asset'];
}
$thumbnailConfiguration = new ThumbnailConfiguration($width, $maximumWidth, $height, $maximumHeight, $allowCropping, $allowUpScaling);
$thumbnailData = $this->assetService->getThumbnailUriAndSizeForAsset($image, $thumbnailConfiguration);
$this->tag->addAttributes(array(
'width' => $thumbnailData['width'],
'height' => $thumbnailData['height'],
'src' => $thumbnailData['src']
));
return $this->tag->render();
}
}