-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
DocumentThumbnailGenerator.php
119 lines (106 loc) · 4.71 KB
/
DocumentThumbnailGenerator.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
<?php
namespace Neos\Media\Domain\Model\ThumbnailGenerator;
/*
* This file is part of the Neos.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 Neos\Media\Domain\Model\Document;
use Neos\Media\Domain\Model\Thumbnail;
use Neos\Media\Exception;
/**
* A system-generated preview version of a Document (PDF, AI and EPS)
*/
class DocumentThumbnailGenerator extends AbstractThumbnailGenerator
{
/**
* The priority for this thumbnail generator.
*
* @var integer
* @api
*/
protected static $priority = 5;
/**
* @param Thumbnail $thumbnail
* @return boolean
*/
public function canRefresh(Thumbnail $thumbnail)
{
return (
$thumbnail->getOriginalAsset() instanceof Document &&
$this->isExtensionSupported($thumbnail) &&
(
$this->imagineService instanceof \Imagine\Imagick\Imagine ||
(extension_loaded('imagick') && $this->getOption('overrideImagineDriverCheck'))
)
);
}
/**
* @param Thumbnail $thumbnail
* @return void
* @throws Exception\NoThumbnailAvailableException
*/
public function refresh(Thumbnail $thumbnail)
{
try {
$filenameWithoutExtension = pathinfo($thumbnail->getOriginalAsset()->getResource()->getFilename(), PATHINFO_FILENAME);
$temporaryLocalCopyFilename = $thumbnail->getOriginalAsset()->getResource()->createTemporaryLocalCopy();
$documentFile = sprintf(in_array($thumbnail->getOriginalAsset()->getResource()->getFileExtension(), $this->getOption('paginableDocuments')) ? '%s[0]' : '%s', $temporaryLocalCopyFilename);
$width = $thumbnail->getConfigurationValue('width') ?: $thumbnail->getConfigurationValue('maximumWidth');
$height = $thumbnail->getConfigurationValue('height') ?: $thumbnail->getConfigurationValue('maximumHeight');
$im = new \Imagick();
$im->setResolution($this->getOption('resolution'), $this->getOption('resolution'));
try {
$readResult = $im->readImage($documentFile);
} catch (\ImagickException $e) {
$readResult = $e;
}
if ($readResult !== true) {
$filename = $thumbnail->getOriginalAsset()->getResource()->getFilename();
$sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1();
$message = $readResult instanceof \ImagickException ? $readResult->getMessage() : 'unknown';
throw new \RuntimeException(
sprintf(
'Could not read image (filename: %s, SHA1: %s) for thumbnail generation. Maybe the ImageMagick security policy denies reading the format? Error: %s',
$filename,
$sha1,
$message
),
1656518085
);
}
$im->setImageFormat('png');
$im->setImageBackgroundColor('white');
$im->setImageCompose(\Imagick::COMPOSITE_OVER);
if (method_exists($im, 'mergeImageLayers')) {
// Replace flattenImages in imagick 3.3.0
// @see https://pecl.php.net/package/imagick/3.3.0RC2
$im = $im->mergeImageLayers(\Imagick::LAYERMETHOD_MERGE);
} else {
$im->flattenImages();
}
if (defined('\Imagick::ALPHACHANNEL_OFF')) {
// ImageMagick >= 7.0, Imagick >= 3.4.3RC1
// @see https://pecl.php.net/package/imagick/3.4.3RC1
$im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_OFF);
} else {
$im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_RESET);
}
$im->thumbnailImage($width, $height, true);
$resource = $this->resourceManager->importResourceFromContent($im->getImageBlob(), $filenameWithoutExtension . '.png');
$im->destroy();
$thumbnail->setResource($resource);
$thumbnail->setWidth($width);
$thumbnail->setHeight($height);
} catch (\Exception $exception) {
$filename = $thumbnail->getOriginalAsset()->getResource()->getFilename();
$sha1 = $thumbnail->getOriginalAsset()->getResource()->getSha1();
$message = sprintf('Unable to generate thumbnail for the given document (filename: %s, SHA1: %s)', $filename, $sha1);
throw new Exception\NoThumbnailAvailableException($message, 1433109652, $exception);
}
}
}