-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumb_engines.php
160 lines (125 loc) · 4.21 KB
/
thumb_engines.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
<?php
/**
* DokuWiki Plugin mediathumbnails (thumb_engine class and subclasses)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Thomas Schäfer <thomas.schaefer@itschert.net>
*/
abstract class thumb_engine {
private ?thumbnail $thumbnail = null;
public function __construct(thumbnail $thumbnail) {
$this->thumbnail = $thumbnail;
}
protected function getSourceFilepath() {
return $this->thumbnail->getSourceFilepath();
}
protected function getTargetFilepath() {
return $this->thumbnail->getFilepath();
}
protected function getTargetMaxDimension() {
return $this->thumbnail->getMaxDimension();
}
public function act() {
if ($this->act_internal()) {
// Set timestamp to the source file's timestamp (this is used to check in later passes if the file already exists in the correct version).
if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) {
touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath()));
}
return true;
}
return false;
}
// Checks if a thumbnail file for the current file version has already been created
protected function thumb_needs_update() {
return !file_exists($this->getTargetFilepath()) || filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath());
}
public abstract function act_internal();
public abstract function getFileSuffix();
}
class thumb_pdf_engine extends thumb_engine {
public function getFileSuffix() {
return "jpg";
}
public function act_internal() {
if ($this->thumb_needs_update()) {
$im = new imagick($this->getSourceFilepath()."[0]");
$im->setImageColorspace(255);
$im->setResolution(300, 300);
$im->setCompressionQuality(95);
$im->setImageFormat('jpeg');
//$im->resizeImage($this->getTargetMaxDimension(),0,imagick::FILTER_LANCZOS,0.9);
//$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
$im->writeImage($this->getTargetFilepath());
$im->clear();
$im->destroy();
// unfortunately, resizeImage or thumbnailImage leads to a black thumbnail in my setup, so I reopen the file and resize it now.
$im = new imagick($this->getTargetFilepath());
$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
$im->writeImage($this->getTargetFilepath());
$im->clear();
$im->destroy();
return true;
} else {
return true;
}
}
}
class thumb_img_engine extends thumb_engine {
public function getFileSuffix() {
return getFileSuffix($this->getSourceFilepath());
}
public function act_internal() {
if ($this->thumb_needs_update()) {
$im = new imagick( $this->getSourceFilepath() );
$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false);
$im->writeImage($this->getTargetFilepath());
$im->clear();
$im->destroy();
return true;
} else {
return true;
}
}
}
class thumb_zip_engine extends thumb_engine {
private array $thumb_paths;
private $file_suffix = "";
public function __construct(thumbnail $thumbnail, array $thumb_paths) {
parent::__construct($thumbnail);
$this->thumb_paths = $thumb_paths;
}
public function getFileSuffix() {
return $this->file_suffix;
}
public function act_internal() {
$zip = new ZipArchive;
if ($zip->open($this->getSourceFilepath()) !== true) {
// file is no zip or cannot be opened
return false;
}
// The media file exists and acts as a zip file!
// Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available
foreach($this->thumb_paths as $thumbnail_path) {
$this->file_suffix = substr(strrchr($thumbnail_path,'.'),1);
if ($zip->locateName($thumbnail_path) !== false) {
if (!$this->thumb_needs_update()) {
return true;
}
// Get the thumbnail file!
$fp = $zip->getStream($thumbnail_path);
if(!$fp) {
return false;
}
$thumbnaildata = '';
while (!feof($fp)) {
$thumbnaildata .= fread($fp, 8192);
}
fclose($fp);
// Write thumbnail file to media folder
file_put_contents($this->getTargetFilepath(), $thumbnaildata);
return true;
}
}
return true;
}
}