-
Notifications
You must be signed in to change notification settings - Fork 0
/
thumbnail.php
164 lines (140 loc) · 5.58 KB
/
thumbnail.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
<?php
/**
* DokuWiki Plugin mediathumbnails (thumbnail class)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Thomas Schäfer <[email protected]>
*/
require('thumb_engines.php');
function getFileSuffix(string $file) {
return substr(strrchr($file,'.'),1);
}
class thumbnail {
private $source_filepath;
private $source_mediapath;
private ?thumb_engine $thumb_engine = null;
private int $max_dimension;
private static $formats;
private static ?bool $pdf_support = null;
private static ?bool $image_support = null;
private static ?bool $no_ghostscript_support = null;
private static ?bool $no_imagick_pdf_readwrite = null;
private static function testDependencies() {
self::$image_support = false;
self::$pdf_support = false;
self::$no_ghostscript_support = false;
self::$no_imagick_pdf_readwrite = false;
if (class_exists ("Imagick")) {
// determine file formats supported by ImageMagick
self::$formats = \Imagick::queryformats();
if (count(self::$formats) > 0) {
self::$image_support = true;
if (in_array("PDF", self::$formats)) {
// Check if GhostScript will answer!
try {
// blank.pdf is an empty reference PDF file to test if GhostScript will react upon loading the file into ImageMagick
$im = new imagick(realpath("lib/plugins/mediathumbnails/blank.pdf")."[0]");
$im->clear();
$im->destroy();
self::$pdf_support = true;
} catch (ImagickException $e) {
if (strpos($e,"PDFDelegateFailed") !== false) {
self::$no_ghostscript_support = true;
}
if (strpos($e,"security policy") !== false) {
self::$no_imagick_pdf_readwrite = true;
}
self::$pdf_support = false;
}
}
}
}
}
public static function supportsPDF() {
if (self::$pdf_support === null) {
self::testDependencies();
}
return self::$pdf_support;
}
public static function supportsImages() {
if (self::$image_support === null) {
self::testDependencies();
}
return self::$image_support;
}
public static function ghostScriptFailed() {
if (self::$no_ghostscript_support === null) {
self::testDependencies();
}
return self::$no_ghostscript_support;
}
public static function imagickPDFpolicyFailed() {
if (self::$no_imagick_pdf_readwrite === null) {
self::testDependencies();
}
return self::$no_imagick_pdf_readwrite;
}
public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) {
if ($ismediapath) {
$this->source_mediapath = $source_filepath;
$this->source_filepath = mediaFN($source_filepath);
} else {
$this->source_mediapath = false;
$this->source_filepath = $source_filepath;
}
$this->max_dimension = $plugin->getConf('thumb_max_dimension');
// Now attach the correct thumb_engine for the file type of the source file
//TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") {
$sourceFileSuffix = getFileSuffix($this->source_filepath);
if ($sourceFileSuffix == "pdf") {
// file suffix is pdf, so assume it's a PDF file
if (self::supportsPDF()) {
$this->thumb_engine = new thumb_pdf_engine($this);
} else {
if (self::ghostScriptFailed()) {
dbg("plugin mediathumbnails: PDF files are supported, but not on this system.\nMost likely, ImageMagick and its PHP extension imagick are installed properly, but GhostScript is not.\nPlease refer to the plugin documentation for a description of the dependencies.");
} else if(self::imagickPDFpolicyFailed()) {
dbg("plugin mediathumbnails: PDF files are supported, but not on this system.\nMost likely, ImageMagick is configured so that PDF conversion is not allowed due to security policies.\nPlease refer to the plugin documentation for a description of the dependencies.");
} else {
dbg("plugin mediathumbnails: PDF files are supported, but not on this system.\nMost likely, ImageMagick or its PHP extension imagick are not installed properly.\nPlease refer to the plugin documentation for a description of the dependencies.");
}
}
} else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) {
// file suffix is in support list of ImageMagick
$this->thumb_engine = new thumb_img_engine($this);
} else if (!self::supportsImages()) {
dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies.");
} else {
// last resort: check if the source file is a ZIP file and look for thumbnails, therein
$this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths'));
}
}
public function getMaxDimension() {
return $this->max_dimension;
}
public function create() {
if (!$this->thumb_engine) {
return false;
}
return $this->thumb_engine->act();
}
public function getSourceFilepath() {
return $this->source_filepath;
}
protected function getFilename() {
return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix();
}
public function getFilepath() {
return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename();
}
public function getMediapath() {
if ($this->source_mediapath !== false) {
return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename();
} else {
return false;
}
}
public function getTimestamp() {
return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false;
}
}