xref: /plugin/mediathumbnails/thumbnail.php (revision 47d2d32a51e008f2a2fbe2231b85ab1545824cca)
1<?php
2/**
3 * DokuWiki Plugin mediathumbnails (thumbnail class)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
7 */
8
9require('thumb_engines.php');
10
11function getFileSuffix(string $file) {
12	return substr(strrchr($file,'.'),1);
13}
14
15class thumbnail {
16
17	private $source_filepath;
18	private $source_mediapath;
19	private ?thumb_engine $thumb_engine = null;
20	private int $max_dimension;
21
22	private static $formats;
23	private static ?bool $pdf_support = null;
24	private static ?bool $image_support = null;
25	private static ?bool $no_ghostscript_support = null;
26
27	private static function testDependencies() {
28		// TODO: move support tests to a Singleton
29		self::$image_support = false;
30		self::$pdf_support = false;
31		self::$no_ghostscript_support = false;
32		if (class_exists ("Imagick")) {
33			// determine file formats supported by ImageMagick
34			self::$formats = \Imagick::queryformats();
35
36			if (count(self::$formats) > 0) {
37				self::$image_support = true;
38				if (in_array("PDF", self::$formats)) {
39					// Check if GhostScript will answer!
40					try {
41						// blank.pdf is an empty reference PDF file to test if GhostScript will react upon loading the file into ImageMagick
42						$im = new imagick(realpath("lib/plugins/mediathumbnails/blank.pdf")."[0]");
43						$im->clear();
44						$im->destroy();
45						self::$pdf_support = true;
46					} catch (ImagickException $e) {
47						if (strpos($e,"PDFDelegateFailed") !== false) {
48							self::$no_ghostscript_support = true;
49						}
50						self::$pdf_support = false;
51					}
52
53				}
54			}
55
56		}
57	}
58	public static function supportsPDF() {
59		if (self::$pdf_support === null) {
60			self::testDependencies();
61		}
62		return self::$pdf_support;
63	}
64	public static function supportsImages() {
65		if (self::$image_support === null) {
66			self::testDependencies();
67		}
68		return self::$image_support;
69	}
70	public static function ghostScriptFailed() {
71		if (self::$no_ghostscript_support === null) {
72			self::testDependencies();
73		}
74		return self::$no_ghostscript_support;
75	}
76
77	public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) {
78
79		if ($ismediapath) {
80			$this->source_mediapath = $source_filepath;
81			$this->source_filepath = mediaFN($source_filepath);
82		} else {
83			$this->source_mediapath = false;
84			$this->source_filepath = $source_filepath;
85		}
86
87		$this->max_dimension = $plugin->getConf('thumb_max_dimension');
88
89		// Now attach the correct thumb_engine for the file type of the source file
90		//TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") {
91		$sourceFileSuffix = getFileSuffix($this->source_filepath);
92		if ($sourceFileSuffix == "pdf") {
93			// file suffix is pdf, so assume it's a PDF file
94			if (self::supportsPDF()) {
95				$this->thumb_engine = new thumb_pdf_engine($this);
96			} else {
97				if (self::ghostScriptFailed()) {
98					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.");
99				} else {
100					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.");
101				}
102			}
103		} else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) {
104			// file suffix is in support list of ImageMagick
105			$this->thumb_engine = new thumb_img_engine($this);
106		} else if (!self::supportsImages()) {
107			dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies.");
108		} else {
109			// last resort: check if the source file is a ZIP file and look for thumbnails, therein
110			$this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths'));
111		}
112	}
113
114	public function getMaxDimension() {
115		return $this->max_dimension;
116	}
117
118	public function create() {
119		if (!$this->thumb_engine) {
120			return false;
121		}
122
123		return $this->thumb_engine->act();
124	}
125
126	public function getSourceFilepath() {
127		return $this->source_filepath;
128	}
129
130	protected function getFilename() {
131
132		return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix();
133	}
134
135	public function getFilepath() {
136		return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename();
137	}
138
139	public function getMediapath() {
140		if ($this->source_mediapath !== false) {
141			return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename();
142		} else {
143			return false;
144		}
145	}
146
147	public function getTimestamp() {
148		return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false;
149	}
150}