xref: /plugin/mediathumbnails/thumbnail.php (revision 086d90eef683a34fe8249b58975c16fbc7ee3200)
147d2d32aSternite<?php
247d2d32aSternite/**
347d2d32aSternite * DokuWiki Plugin mediathumbnails (thumbnail class)
447d2d32aSternite *
547d2d32aSternite * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
647d2d32aSternite * @author  Thomas Schäfer <thomas.schaefer@itschert.net>
747d2d32aSternite */
847d2d32aSternite
947d2d32aSterniterequire('thumb_engines.php');
1047d2d32aSternite
1147d2d32aSternitefunction getFileSuffix(string $file) {
1247d2d32aSternite	return substr(strrchr($file,'.'),1);
1347d2d32aSternite}
1447d2d32aSternite
1547d2d32aSterniteclass thumbnail {
1647d2d32aSternite
1747d2d32aSternite	private $source_filepath;
1847d2d32aSternite	private $source_mediapath;
1947d2d32aSternite	private ?thumb_engine $thumb_engine = null;
2047d2d32aSternite	private int $max_dimension;
2147d2d32aSternite
2247d2d32aSternite	private static $formats;
2347d2d32aSternite	private static ?bool $pdf_support = null;
2447d2d32aSternite	private static ?bool $image_support = null;
2547d2d32aSternite	private static ?bool $no_ghostscript_support = null;
2647d2d32aSternite
2747d2d32aSternite	private static function testDependencies() {
28*086d90eeSternite
2947d2d32aSternite		self::$image_support = false;
3047d2d32aSternite		self::$pdf_support = false;
3147d2d32aSternite		self::$no_ghostscript_support = false;
32*086d90eeSternite
3347d2d32aSternite		if (class_exists ("Imagick")) {
3447d2d32aSternite			// determine file formats supported by ImageMagick
3547d2d32aSternite			self::$formats = \Imagick::queryformats();
3647d2d32aSternite
3747d2d32aSternite			if (count(self::$formats) > 0) {
3847d2d32aSternite				self::$image_support = true;
3947d2d32aSternite				if (in_array("PDF", self::$formats)) {
4047d2d32aSternite					// Check if GhostScript will answer!
4147d2d32aSternite					try {
4247d2d32aSternite						// blank.pdf is an empty reference PDF file to test if GhostScript will react upon loading the file into ImageMagick
4347d2d32aSternite						$im = new imagick(realpath("lib/plugins/mediathumbnails/blank.pdf")."[0]");
4447d2d32aSternite						$im->clear();
4547d2d32aSternite						$im->destroy();
4647d2d32aSternite						self::$pdf_support = true;
4747d2d32aSternite					} catch (ImagickException $e) {
4847d2d32aSternite						if (strpos($e,"PDFDelegateFailed") !== false) {
4947d2d32aSternite							self::$no_ghostscript_support = true;
5047d2d32aSternite						}
5147d2d32aSternite						self::$pdf_support = false;
5247d2d32aSternite					}
5347d2d32aSternite
5447d2d32aSternite				}
5547d2d32aSternite			}
5647d2d32aSternite
5747d2d32aSternite		}
5847d2d32aSternite	}
5947d2d32aSternite	public static function supportsPDF() {
6047d2d32aSternite		if (self::$pdf_support === null) {
6147d2d32aSternite			self::testDependencies();
6247d2d32aSternite		}
6347d2d32aSternite		return self::$pdf_support;
6447d2d32aSternite	}
6547d2d32aSternite	public static function supportsImages() {
6647d2d32aSternite		if (self::$image_support === null) {
6747d2d32aSternite			self::testDependencies();
6847d2d32aSternite		}
6947d2d32aSternite		return self::$image_support;
7047d2d32aSternite	}
7147d2d32aSternite	public static function ghostScriptFailed() {
7247d2d32aSternite		if (self::$no_ghostscript_support === null) {
7347d2d32aSternite			self::testDependencies();
7447d2d32aSternite		}
7547d2d32aSternite		return self::$no_ghostscript_support;
7647d2d32aSternite	}
7747d2d32aSternite
7847d2d32aSternite	public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) {
7947d2d32aSternite
8047d2d32aSternite		if ($ismediapath) {
8147d2d32aSternite			$this->source_mediapath = $source_filepath;
8247d2d32aSternite			$this->source_filepath = mediaFN($source_filepath);
8347d2d32aSternite		} else {
8447d2d32aSternite			$this->source_mediapath = false;
8547d2d32aSternite			$this->source_filepath = $source_filepath;
8647d2d32aSternite		}
8747d2d32aSternite
8847d2d32aSternite		$this->max_dimension = $plugin->getConf('thumb_max_dimension');
8947d2d32aSternite
9047d2d32aSternite		// Now attach the correct thumb_engine for the file type of the source file
9147d2d32aSternite		//TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") {
9247d2d32aSternite		$sourceFileSuffix = getFileSuffix($this->source_filepath);
9347d2d32aSternite		if ($sourceFileSuffix == "pdf") {
9447d2d32aSternite			// file suffix is pdf, so assume it's a PDF file
9547d2d32aSternite			if (self::supportsPDF()) {
9647d2d32aSternite				$this->thumb_engine = new thumb_pdf_engine($this);
9747d2d32aSternite			} else {
9847d2d32aSternite				if (self::ghostScriptFailed()) {
9947d2d32aSternite					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.");
10047d2d32aSternite				} else {
10147d2d32aSternite					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.");
10247d2d32aSternite				}
10347d2d32aSternite			}
10447d2d32aSternite		} else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) {
10547d2d32aSternite			// file suffix is in support list of ImageMagick
10647d2d32aSternite			$this->thumb_engine = new thumb_img_engine($this);
10747d2d32aSternite		} else if (!self::supportsImages()) {
10847d2d32aSternite			dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies.");
10947d2d32aSternite		} else {
11047d2d32aSternite			// last resort: check if the source file is a ZIP file and look for thumbnails, therein
11147d2d32aSternite			$this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths'));
11247d2d32aSternite		}
11347d2d32aSternite	}
11447d2d32aSternite
11547d2d32aSternite	public function getMaxDimension() {
11647d2d32aSternite		return $this->max_dimension;
11747d2d32aSternite	}
11847d2d32aSternite
11947d2d32aSternite	public function create() {
12047d2d32aSternite		if (!$this->thumb_engine) {
12147d2d32aSternite			return false;
12247d2d32aSternite		}
12347d2d32aSternite
12447d2d32aSternite		return $this->thumb_engine->act();
12547d2d32aSternite	}
12647d2d32aSternite
12747d2d32aSternite	public function getSourceFilepath() {
12847d2d32aSternite		return $this->source_filepath;
12947d2d32aSternite	}
13047d2d32aSternite
13147d2d32aSternite	protected function getFilename() {
13247d2d32aSternite
13347d2d32aSternite		return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix();
13447d2d32aSternite	}
13547d2d32aSternite
13647d2d32aSternite	public function getFilepath() {
13747d2d32aSternite		return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename();
13847d2d32aSternite	}
13947d2d32aSternite
14047d2d32aSternite	public function getMediapath() {
14147d2d32aSternite		if ($this->source_mediapath !== false) {
14247d2d32aSternite			return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename();
14347d2d32aSternite		} else {
14447d2d32aSternite			return false;
14547d2d32aSternite		}
14647d2d32aSternite	}
14747d2d32aSternite
14847d2d32aSternite	public function getTimestamp() {
14947d2d32aSternite		return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false;
15047d2d32aSternite	}
15147d2d32aSternite}