/
ImageThumbnail.php
307 lines (261 loc) · 7.77 KB
/
ImageThumbnail.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @category Pimcore
* @package Asset
* @copyright Copyright (c) 2009-2016 pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Model\Asset\Document;
use Pimcore\Model\Asset\Image;
use Pimcore\Model;
use Pimcore\File;
class ImageThumbnail
{
/**
* @var \Pimcore\Model\Asset\Video
*/
protected $asset;
/**
* @var mixed|string
*/
protected $filesystemPath;
/**
* @var int
*/
protected $width;
/**
* @var int
*/
protected $height;
/**
* @var int
*/
protected $realWidth;
/**
* @var int
*/
protected $realHeight;
/**
* @var Image\Thumbnail\Config
*/
protected $config;
/**
* @var bool
*/
protected $deferred = true;
/**
* @var int
*/
protected $page = 1;
/**
* ImageThumbnail constructor.
* @param $asset
* @param $config
* @param int $page
* @param bool $deferred
*/
public function __construct($asset, $config = null, $page = 1, $deferred = true)
{
$this->asset = $asset;
$this->config = $this->createConfig($config);
$this->page = $page;
$this->deferred = $deferred;
}
/**
* @return mixed
*/
public function getPath()
{
$fsPath = $this->getFileSystemPath();
$path = str_replace(PIMCORE_DOCUMENT_ROOT, "", $fsPath);
$results = \Pimcore::getEventManager()->trigger("frontend.path.asset.document.image-thumbnail", $this, [
"filesystemPath" => $fsPath,
"frontendPath" => $path
]);
if ($results->count()) {
$path = $results->last();
}
return $path;
}
/**
* @return mixed|string
*/
public function getFileSystemPath()
{
$this->generate();
return $this->filesystemPath;
}
/**
*
*/
public function generate()
{
$errorImage = PIMCORE_PATH . '/static6/img/filetype-not-supported.png';
if (!$this->asset) {
$this->filesystemPath = $errorImage;
} elseif (!$this->filesystemPath) {
$config = $this->getConfig();
$config->setName("document_" . $config->getName()."-".$this->page);
try {
$path = null;
if (!$this->deferred) {
$converter = \Pimcore\Document::getInstance();
$converter->load($this->asset->getFileSystemPath());
$path = PIMCORE_TEMPORARY_DIRECTORY . "/document-image-cache/document_" . $this->asset->getId() . "__thumbnail_" . $this->page . ".png";
if (!is_dir(dirname($path))) {
\Pimcore\File::mkdir(dirname($path));
}
$lockKey = "document-thumbnail-" . $this->asset->getId() . "-" . $this->page;
if (!is_file($path) && !Model\Tool\Lock::isLocked($lockKey)) {
Model\Tool\Lock::lock($lockKey);
$converter->saveImage($path, $this->page);
Model\Tool\Lock::release($lockKey);
} elseif (Model\Tool\Lock::isLocked($lockKey)) {
return "/pimcore/static6/img/please-wait.png";
}
}
if ($config) {
$path = Image\Thumbnail\Processor::process($this->asset, $config, $path, $this->deferred, true);
}
$this->filesystemPath = $path;
\Pimcore::getEventManager()->trigger("asset.document.image-thumbnail", $this);
} catch (\Exception $e) {
\Logger::error("Couldn't create image-thumbnail of document " . $this->asset->getRealFullPath());
\Logger::error($e);
$this->filesystemPath = $errorImage;
}
}
}
/**
*
*/
public function reset()
{
$this->filesystemPath = null;
$this->width = null;
$this->height = null;
$this->realHeight = null;
$this->realWidth = null;
}
/**
* Get the public path to the thumbnail image.
* This method is here for backwards compatility.
* Up to Pimcore 1.4.8 a thumbnail was returned as a path to an image.
* @return string Public path to thumbnail image.
*/
public function __toString()
{
return $this->getPath();
}
/**
* @return int Width of the generated thumbnail image.
*/
public function getWidth()
{
if (!$this->width) {
$this->getDimensions();
}
return $this->width;
}
/**
* Get the width of the generated thumbnail image in pixels.
* @return int Height of the generated thumbnail image.
*/
public function getHeight()
{
if (!$this->height) {
$this->getDimensions();
}
return $this->height;
}
/**
* @return int real Width of the generated thumbnail image. (when using high resolution option)
*/
public function getRealWidth()
{
if (!$this->realWidth) {
$this->getDimensions();
}
return $this->realWidth;
}
/**
* Get the real width of the generated thumbnail image in pixels. (when using high resolution option)
* @return int Height of the generated thumbnail image.
*/
public function getRealHeight()
{
if (!$this->realHeight) {
$this->getDimensions();
}
return $this->realHeight;
}
/**
* @return array
*/
public function getDimensions()
{
if (!$this->width || !$this->height) {
$config = $this->getConfig();
$dimensions = [];
// generate the thumbnail and get dimensions from the thumbnail file
$info = @getimagesize($this->getFileSystemPath());
if ($info) {
$dimensions = [
"width" => $info[0],
"height" => $info[1]
];
}
$this->width = $dimensions["width"];
$this->height = $dimensions["height"];
// the following is only relevant if using high-res option (retina, ...)
$this->realHeight = $this->height;
$this->realWidth = $this->width;
if ($config && $config->getHighResolution() && $config->getHighResolution() > 1) {
$this->realWidth = floor($this->width * $config->getHighResolution());
$this->realHeight = floor($this->height * $config->getHighResolution());
}
}
return [
"width" => $this->width,
"height" => $this->height
];
}
/**
* @return \Pimcore\Model\Asset\Image The original image from which this thumbnail is generated.
*/
public function getAsset()
{
return $this->asset;
}
/**
* Get thumbnail image configuration.
* @return Image\Thumbnail\Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @param $selector
* @return bool|static
*/
protected function createConfig($selector)
{
$config = Image\Thumbnail\Config::getByAutoDetect($selector);
if ($config) {
$format = strtolower($config->getFormat());
if ($format == "source") {
$config->setFormat("PNG");
}
}
return $config;
}
}