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