xref: /plugin/mediathumbnails/thumbnail.php (revision 5fa23922f2bd9e9a55a418dfab6040a327e8c436)
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;
26*5fa23922Sternite	private static ?bool $no_imagick_pdf_readwrite = null;
2747d2d32aSternite
2847d2d32aSternite	private static function testDependencies() {
29086d90eeSternite
3047d2d32aSternite		self::$image_support = false;
3147d2d32aSternite		self::$pdf_support = false;
3247d2d32aSternite		self::$no_ghostscript_support = false;
33*5fa23922Sternite		self::$no_imagick_pdf_readwrite = false;
34086d90eeSternite
3547d2d32aSternite		if (class_exists ("Imagick")) {
3647d2d32aSternite			// determine file formats supported by ImageMagick
3747d2d32aSternite			self::$formats = \Imagick::queryformats();
3847d2d32aSternite
3947d2d32aSternite			if (count(self::$formats) > 0) {
4047d2d32aSternite				self::$image_support = true;
4147d2d32aSternite				if (in_array("PDF", self::$formats)) {
4247d2d32aSternite					// Check if GhostScript will answer!
4347d2d32aSternite					try {
4447d2d32aSternite						// blank.pdf is an empty reference PDF file to test if GhostScript will react upon loading the file into ImageMagick
4547d2d32aSternite						$im = new imagick(realpath("lib/plugins/mediathumbnails/blank.pdf")."[0]");
4647d2d32aSternite						$im->clear();
4747d2d32aSternite						$im->destroy();
4847d2d32aSternite						self::$pdf_support = true;
4947d2d32aSternite					} catch (ImagickException $e) {
5047d2d32aSternite						if (strpos($e,"PDFDelegateFailed") !== false) {
5147d2d32aSternite							self::$no_ghostscript_support = true;
5247d2d32aSternite						}
53*5fa23922Sternite						if (strpos($e,"security policy") !== false) {
54*5fa23922Sternite							self::$no_imagick_pdf_readwrite = true;
55*5fa23922Sternite						}
5647d2d32aSternite						self::$pdf_support = false;
5747d2d32aSternite					}
5847d2d32aSternite
5947d2d32aSternite				}
6047d2d32aSternite			}
6147d2d32aSternite
6247d2d32aSternite		}
6347d2d32aSternite	}
6447d2d32aSternite	public static function supportsPDF() {
6547d2d32aSternite		if (self::$pdf_support === null) {
6647d2d32aSternite			self::testDependencies();
6747d2d32aSternite		}
6847d2d32aSternite		return self::$pdf_support;
6947d2d32aSternite	}
7047d2d32aSternite	public static function supportsImages() {
7147d2d32aSternite		if (self::$image_support === null) {
7247d2d32aSternite			self::testDependencies();
7347d2d32aSternite		}
7447d2d32aSternite		return self::$image_support;
7547d2d32aSternite	}
7647d2d32aSternite	public static function ghostScriptFailed() {
7747d2d32aSternite		if (self::$no_ghostscript_support === null) {
7847d2d32aSternite			self::testDependencies();
7947d2d32aSternite		}
8047d2d32aSternite		return self::$no_ghostscript_support;
8147d2d32aSternite	}
82*5fa23922Sternite	public static function imagickPDFpolicyFailed() {
83*5fa23922Sternite		if (self::$no_imagick_pdf_readwrite === null) {
84*5fa23922Sternite			self::testDependencies();
85*5fa23922Sternite		}
86*5fa23922Sternite		return self::$no_imagick_pdf_readwrite;
87*5fa23922Sternite	}
8847d2d32aSternite
8947d2d32aSternite	public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) {
9047d2d32aSternite
9147d2d32aSternite		if ($ismediapath) {
9247d2d32aSternite			$this->source_mediapath = $source_filepath;
9347d2d32aSternite			$this->source_filepath = mediaFN($source_filepath);
9447d2d32aSternite		} else {
9547d2d32aSternite			$this->source_mediapath = false;
9647d2d32aSternite			$this->source_filepath = $source_filepath;
9747d2d32aSternite		}
9847d2d32aSternite
9947d2d32aSternite		$this->max_dimension = $plugin->getConf('thumb_max_dimension');
10047d2d32aSternite
10147d2d32aSternite		// Now attach the correct thumb_engine for the file type of the source file
10247d2d32aSternite		//TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") {
10347d2d32aSternite		$sourceFileSuffix = getFileSuffix($this->source_filepath);
10447d2d32aSternite		if ($sourceFileSuffix == "pdf") {
10547d2d32aSternite			// file suffix is pdf, so assume it's a PDF file
10647d2d32aSternite			if (self::supportsPDF()) {
10747d2d32aSternite				$this->thumb_engine = new thumb_pdf_engine($this);
10847d2d32aSternite			} else {
10947d2d32aSternite				if (self::ghostScriptFailed()) {
11047d2d32aSternite					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.");
111*5fa23922Sternite				} else if(self::imagickPDFpolicyFailed()) {
112*5fa23922Sternite					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.");
11347d2d32aSternite				} else {
11447d2d32aSternite					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.");
11547d2d32aSternite				}
11647d2d32aSternite			}
11747d2d32aSternite		} else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) {
11847d2d32aSternite			// file suffix is in support list of ImageMagick
11947d2d32aSternite			$this->thumb_engine = new thumb_img_engine($this);
12047d2d32aSternite		} else if (!self::supportsImages()) {
12147d2d32aSternite			dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies.");
12247d2d32aSternite		} else {
12347d2d32aSternite			// last resort: check if the source file is a ZIP file and look for thumbnails, therein
12447d2d32aSternite			$this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths'));
12547d2d32aSternite		}
12647d2d32aSternite	}
12747d2d32aSternite
12847d2d32aSternite	public function getMaxDimension() {
12947d2d32aSternite		return $this->max_dimension;
13047d2d32aSternite	}
13147d2d32aSternite
13247d2d32aSternite	public function create() {
13347d2d32aSternite		if (!$this->thumb_engine) {
13447d2d32aSternite			return false;
13547d2d32aSternite		}
13647d2d32aSternite
13747d2d32aSternite		return $this->thumb_engine->act();
13847d2d32aSternite	}
13947d2d32aSternite
14047d2d32aSternite	public function getSourceFilepath() {
14147d2d32aSternite		return $this->source_filepath;
14247d2d32aSternite	}
14347d2d32aSternite
14447d2d32aSternite	protected function getFilename() {
14547d2d32aSternite
14647d2d32aSternite		return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix();
14747d2d32aSternite	}
14847d2d32aSternite
14947d2d32aSternite	public function getFilepath() {
15047d2d32aSternite		return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename();
15147d2d32aSternite	}
15247d2d32aSternite
15347d2d32aSternite	public function getMediapath() {
15447d2d32aSternite		if ($this->source_mediapath !== false) {
15547d2d32aSternite			return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename();
15647d2d32aSternite		} else {
15747d2d32aSternite			return false;
15847d2d32aSternite		}
15947d2d32aSternite	}
16047d2d32aSternite
16147d2d32aSternite	public function getTimestamp() {
16247d2d32aSternite		return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false;
16347d2d32aSternite	}
16447d2d32aSternite}