1<?php 2/** 3 * DokuWiki Plugin mediathumbnails (Syntax Component) 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 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) { 11 die(); 12} 13 14class syntax_plugin_mediathumbnails extends DokuWiki_Syntax_Plugin { 15 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType() 20 { 21 return 'substition'; 22 } 23 24 /** 25 * @return string Paragraph type 26 */ 27 public function getPType() 28 { 29 return 'normal'; 30 } 31 32 /** 33 * @return int Sort order - Low numbers go before high numbers 34 */ 35 public function getSort() 36 { 37 return 1; 38 } 39 40 /** 41 * Connect lookup pattern to lexer. 42 * 43 * @param string $mode Parser mode 44 */ 45 public function connectTo($mode) 46 { 47 $this->Lexer->addSpecialPattern("{{thumbnail>.+?}}", $mode, substr(get_class($this), 7)); 48 } 49 50 /** 51 * Handle matches of the mediathumbnails syntax 52 * 53 * @param string $match The match of the syntax 54 * @param int $state The state of the handler 55 * @param int $pos The position in the document 56 * @param Doku_Handler $handler The handler 57 * 58 * @return array Data for the renderer 59 */ 60 public function handle($match, $state, $pos, Doku_Handler $handler) 61 { 62 // Locate the given media file and check if it can be opened as zip 63 $mediapath_file = substr($match, 12, -2); //strip markup 64 65 $thumb = new thumbnail($mediapath_file,$this); 66 if ($thumb->create()) { 67 return array($mediapath_file,$thumb->getMediapath()); 68 } 69 70 return array($mediapath_file); 71 } 72 73 /** 74 * Render xhtml output or metadata 75 * 76 * @param string $mode Renderer mode (supported modes: xhtml) 77 * @param Doku_Renderer $renderer The renderer 78 * @param array $data The data from the handler() function 79 * 80 * @return bool If rendering was successful. 81 */ 82 public function render($mode, Doku_Renderer $renderer, $data) 83 { 84 list ($mediapath_file, $mediapath_thumbnail) = $data; 85 86 if ($mode == 'xhtml') { 87 88 // check if a thumbnail file was found 89 if (!$mediapath_thumbnail) { 90 if ($this->getConf('show_missing_thumb_error')) { 91 $renderer->doc .= trim($this->getConf('no_thumb_error_message')) . " " . $mediapath_file; 92 return true; 93 } else { 94 return false; 95 } 96 } 97 98 $src = ml($mediapath_thumbnail,array()); 99 100 $i = array(); 101 $i['width'] = $this->getConf('thumb_max_dimension'); //TODO: ausrichtung herausrechnen! 102 //$i['height'] = ''; 103 $i['title'] = $mediapath_file; 104 $i['class'] = 'tn'; 105 $iatt = buildAttributes($i); 106 107 $renderer->doc .= ($this->getConf('link_to_media_file') ? '<a href="/lib/exe/fetch.php?media=' . $mediapath_file . '">' : '') . 108 '<img src="'.$src.'" '.$iatt.' />' . 109 ($this->getConf('link_to_media_file') ? '</a>' : ''); 110 return true; 111 112 } elseif ($mode == 'odt') { 113 114 // TODO: yet to implement 115 $renderer->cdata(""); 116 return true; 117 118 } 119 120 return false; 121 } 122} 123 124function getFileSuffix(string $file) { 125 return substr(strrchr($file,'.'),1); 126} 127 128class thumbnail { 129 130 private $source_filepath; 131 private $source_mediapath; 132 private ?thumb_engine $thumb_engine = null; 133 private int $max_dimension; 134 135 private static $formats; 136 private static ?bool $pdf_support = null; 137 private static ?bool $image_support = null; 138 private static ?bool $no_ghostscript_support = null; 139 140 private static function testDependencies() { 141 // TODO: move support tests to a Singleton 142 self::$image_support = false; 143 self::$pdf_support = false; 144 self::$no_ghostscript_support = false; 145 if (class_exists ("Imagick")) { 146 // determine file formats supported by ImageMagick 147 self::$formats = \Imagick::queryformats(); 148 149 if (count(self::$formats) > 0) { 150 self::$image_support = true; 151 if (in_array("PDF", self::$formats)) { 152 // Check if GhostScript will answer! 153 try { 154 $im = new imagick("D:\\SynologyPraxis\\Praxiswiki\\dokuwiki\\data\\media\\intern\\texbuch.pdf"."[0]"); 155 $im->clear(); 156 $im->destroy(); 157 self::$pdf_support = true; 158 } catch (ImagickException $e) { 159 if (strpos($e,"PDFDelegateFailed") !== false) { 160 self::$no_ghostscript_support = true; 161 } 162 self::$pdf_support = false; 163 } 164 165 } 166 } 167 168 } 169 } 170 public static function supportsPDF() { 171 if (self::$pdf_support === null) { 172 self::testDependencies(); 173 } 174 return self::$pdf_support; 175 } 176 public static function supportsImages() { 177 if (self::$image_support === null) { 178 self::testDependencies(); 179 } 180 return self::$image_support; 181 } 182 public static function ghostScriptFailed() { 183 if (self::$no_ghostscript_support === null) { 184 self::testDependencies(); 185 } 186 return self::$no_ghostscript_support; 187 } 188 189 public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) { 190 191 if ($ismediapath) { 192 $this->source_mediapath = $source_filepath; 193 $this->source_filepath = mediaFN($source_filepath); 194 } else { 195 $this->source_mediapath = false; 196 $this->source_filepath = $source_filepath; 197 } 198 199 $this->max_dimension = $plugin->getConf('thumb_max_dimension'); 200 201 202 203 // Now attach the correct thumb_engine for the file type of the source file 204 //TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") { 205 $sourceFileSuffix = getFileSuffix($this->source_filepath); 206 if ($sourceFileSuffix == "pdf") { 207 // file suffix is pdf, so assume it's a PDF file 208 if (self::supportsPDF()) { 209 $this->thumb_engine = new thumb_pdf_engine($this); 210 } else { 211 if (self::ghostScriptFailed()) { 212 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."); 213 } else { 214 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."); 215 } 216 } 217 } else if (self::supportsImages() && in_array(strtoupper($sourceFileSuffix), self::$formats)) { 218 // file suffix is in support list of ImageMagick 219 $this->thumb_engine = new thumb_img_engine($this); 220 } else if (!self::supportsImages()) { 221 dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies."); 222 } else { 223 // last resort: check if the source file is a ZIP file and look for thumbnails, therein 224 $this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths')); 225 } 226 } 227 228 public function getMaxDimension() { 229 return $this->max_dimension; 230 } 231 232 public function create() { 233 if (!$this->thumb_engine) { 234 return false; 235 } 236 237 return $this->thumb_engine->act(); 238 } 239 240 public function getSourceFilepath() { 241 return $this->source_filepath; 242 } 243 244 protected function getFilename() { 245 246 return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix(); 247 } 248 249 public function getFilepath() { 250 return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename(); 251 } 252 253 public function getMediapath() { 254 if ($this->source_mediapath !== false) { 255 return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename(); 256 } else { 257 return false; 258 } 259 } 260 261 public function getTimestamp() { 262 return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false; 263 } 264} 265 266abstract class thumb_engine { 267 268 private ?thumbnail $thumbnail = null; 269 270 public function __construct(thumbnail $thumbnail) { 271 $this->thumbnail = $thumbnail; 272 } 273 274 protected function getSourceFilepath() { 275 return $this->thumbnail->getSourceFilepath(); 276 } 277 278 protected function getTargetFilepath() { 279 return $this->thumbnail->getFilepath(); 280 } 281 282 protected function getTargetMaxDimension() { 283 return $this->thumbnail->getMaxDimension(); 284 } 285 286 public function act() { 287 if ($this->act_internal()) { 288 // 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). 289 if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) { 290 touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath())); 291 } 292 return true; 293 } 294 return false; 295 } 296 297 // Checks if a thumbnail file for the current file version has already been created 298 protected function thumb_needs_update() { 299 return !file_exists($this->getTargetFilepath()) || filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath()); 300 } 301 302 public abstract function act_internal(); 303 304 public abstract function getFileSuffix(); 305} 306 307class thumb_pdf_engine extends thumb_engine { 308 309 public function getFileSuffix() { 310 return "jpg"; 311 } 312 313 public function act_internal() { 314 if ($this->thumb_needs_update()) { 315 $im = new imagick($this->getSourceFilepath()."[0]"); 316 $im->setImageColorspace(255); 317 $im->setResolution(300, 300); 318 $im->setCompressionQuality(95); 319 $im->setImageFormat('jpeg'); 320 //$im->resizeImage($this->getTargetMaxDimension(),0,imagick::FILTER_LANCZOS,0.9); 321 //$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 322 $im->writeImage($this->getTargetFilepath()); 323 $im->clear(); 324 $im->destroy(); 325 326 // unfortunately, resizeImage or thumbnailImage leads to a black thumbnail in my setup, so I reopen the file and resize it now. 327 $im = new imagick($this->getTargetFilepath()); 328 $im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 329 $im->writeImage($this->getTargetFilepath()); 330 $im->clear(); 331 $im->destroy(); 332 333 return true; 334 } else { 335 return true; 336 } 337 } 338} 339 340class thumb_img_engine extends thumb_engine { 341 342 public function getFileSuffix() { 343 return getFileSuffix($this->getSourceFilepath()); 344 } 345 346 public function act_internal() { 347 if ($this->thumb_needs_update()) { 348 $im = new imagick( $this->getSourceFilepath() ); 349 $im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 350 $im->writeImage($this->getTargetFilepath()); 351 $im->clear(); 352 $im->destroy(); 353 354 return true; 355 } else { 356 return true; 357 } 358 } 359} 360 361class thumb_zip_engine extends thumb_engine { 362 363 private array $thumb_paths; 364 private $file_suffix = ""; 365 366 public function __construct(thumbnail $thumbnail, array $thumb_paths) { 367 parent::__construct($thumbnail); 368 $this->thumb_paths = $thumb_paths; 369 } 370 371 public function getFileSuffix() { 372 return $this->file_suffix; 373 } 374 375 public function act_internal() { 376 377 $zip = new ZipArchive; 378 if ($zip->open($this->getSourceFilepath()) !== true) { 379 // file is no zip or cannot be opened 380 return false; 381 } 382 383 // The media file exists and acts as a zip file! 384 385 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 386 foreach($this->thumb_paths as $thumbnail_path) { 387 $this->file_suffix = substr(strrchr($thumbnail_path,'.'),1); 388 389 if ($zip->locateName($thumbnail_path) !== false) { 390 391 if (!$this->thumb_needs_update()) { 392 return true; 393 } 394 395 // Get the thumbnail file! 396 $fp = $zip->getStream($thumbnail_path); 397 if(!$fp) { 398 return false; 399 } 400 401 $thumbnaildata = ''; 402 while (!feof($fp)) { 403 $thumbnaildata .= fread($fp, 8192); 404 } 405 406 fclose($fp); 407 408 // Write thumbnail file to media folder 409 file_put_contents($this->getTargetFilepath(), $thumbnaildata); 410 411 return true; 412 } 413 } 414 415 return true; 416 } 417}