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 $formats; 134 private int $max_dimension; 135 136 public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) { 137 138 if ($ismediapath) { 139 $this->source_mediapath = $source_filepath; 140 $this->source_filepath = mediaFN($source_filepath); 141 } else { 142 $this->source_mediapath = false; 143 $this->source_filepath = $source_filepath; 144 } 145 146 $this->max_dimension = $plugin->getConf('thumb_max_dimension'); 147 148 // TODO: move support tests to a Singleton 149 $image_support = false; 150 $pdf_support = false; 151 $no_ghostscript = false; 152 if (class_exists ("Imagick")) { 153 // determine file formats supported by ImageMagick 154 $this->formats = \Imagick::queryformats(); 155 156 if (count($this->formats) > 0) { 157 $image_support = true; 158 if (in_array("PDF", $this->formats)) { 159 // Check if GhostScript will answer! 160 try { 161 $im = new imagick($this->source_filepath."[0]"); 162 $im->clear(); 163 $im->destroy(); 164 $pdf_support = true; 165 } catch (ImagickException $e) { 166 if (strpos($e,"PDFDelegateFailed") !== false) { 167 $no_ghostscript = true; 168 } 169 $pdf_support = false; 170 } 171 172 } 173 } 174 175 } 176 177 // Now attach the correct thumb_engine for the file type of the source file 178 //TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") { 179 $sourceFileSuffix = getFileSuffix($this->source_filepath); 180 if ($sourceFileSuffix == "pdf") { 181 // file suffix is pdf, so assume it's a PDF file 182 if ($pdf_support) { 183 $this->thumb_engine = new thumb_pdf_engine($this); 184 } else { 185 if ($no_ghostscript) { 186 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."); 187 } else { 188 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."); 189 } 190 } 191 } else if ($image_support && in_array(strtoupper($sourceFileSuffix), $this->formats)) { 192 // file suffix is in support list of ImageMagick 193 $this->thumb_engine = new thumb_img_engine($this); 194 } else if (!$image_support) { 195 dbg("plugin mediathumbnails: Image files are supported, but not on this system.\nPlease refer to the plugin documentation for a description of the dependencies."); 196 } else { 197 // last resort: check if the source file is a ZIP file and look for thumbnails, therein 198 $this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_paths')); 199 } 200 } 201 202 public function getMaxDimension() { 203 return $this->max_dimension; 204 } 205 206 public function create() { 207 if (!$this->thumb_engine) { 208 return false; 209 } 210 211 return $this->thumb_engine->act(); 212 } 213 214 public function getSourceFilepath() { 215 return $this->source_filepath; 216 } 217 218 protected function getFilename() { 219 220 return basename($this->source_filepath) . ".thumb".$this->max_dimension.".".$this->thumb_engine->getFileSuffix(); 221 } 222 223 public function getFilepath() { 224 return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename(); 225 } 226 227 public function getMediapath() { 228 if ($this->source_mediapath !== false) { 229 return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename(); 230 } else { 231 return false; 232 } 233 } 234 235 public function getTimestamp() { 236 return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false; 237 } 238} 239 240abstract class thumb_engine { 241 242 private ?thumbnail $thumbnail = null; 243 244 public function __construct(thumbnail $thumbnail) { 245 $this->thumbnail = $thumbnail; 246 } 247 248 protected function getSourceFilepath() { 249 return $this->thumbnail->getSourceFilepath(); 250 } 251 252 protected function getTargetFilepath() { 253 return $this->thumbnail->getFilepath(); 254 } 255 256 protected function getTargetMaxDimension() { 257 return $this->thumbnail->getMaxDimension(); 258 } 259 260 public function act() { 261 if ($this->act_internal()) { 262 // 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). 263 if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) { 264 touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath())); 265 } 266 return true; 267 } 268 return false; 269 } 270 271 // Checks if a thumbnail file for the current file version has already been created 272 protected function thumb_needs_update() { 273 return !file_exists($this->getTargetFilepath()) || filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath()); 274 } 275 276 public abstract function act_internal(); 277 278 public abstract function getFileSuffix(); 279} 280 281class thumb_pdf_engine extends thumb_engine { 282 283 public function getFileSuffix() { 284 return "jpg"; 285 } 286 287 public function act_internal() { 288 if ($this->thumb_needs_update()) { 289 $im = new imagick($this->getSourceFilepath()."[0]"); 290 $im->setImageColorspace(255); 291 $im->setResolution(300, 300); 292 $im->setCompressionQuality(95); 293 $im->setImageFormat('jpeg'); 294 //$im->resizeImage($this->getTargetMaxDimension(),0,imagick::FILTER_LANCZOS,0.9); 295 //$im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 296 $im->writeImage($this->getTargetFilepath()); 297 $im->clear(); 298 $im->destroy(); 299 300 // unfortunately, resizeImage or thumbnailImage leads to a black thumbnail in my setup, so I reopen the file and resize it now. 301 $im = new imagick($this->getTargetFilepath()); 302 $im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 303 $im->writeImage($this->getTargetFilepath()); 304 $im->clear(); 305 $im->destroy(); 306 307 return true; 308 } else { 309 return true; 310 } 311 } 312} 313 314class thumb_img_engine extends thumb_engine { 315 316 public function getFileSuffix() { 317 return getFileSuffix($this->getSourceFilepath()); 318 } 319 320 public function act_internal() { 321 if ($this->thumb_needs_update()) { 322 $im = new imagick( $this->getSourceFilepath() ); 323 $im->thumbnailImage($this->getTargetMaxDimension(),$this->getTargetMaxDimension(),true,false); 324 $im->writeImage($this->getTargetFilepath()); 325 $im->clear(); 326 $im->destroy(); 327 328 return true; 329 } else { 330 return true; 331 } 332 } 333} 334 335class thumb_zip_engine extends thumb_engine { 336 337 private array $thumb_paths; 338 private $file_suffix = ""; 339 340 public function __construct(thumbnail $thumbnail, array $thumb_paths) { 341 parent::__construct($thumbnail); 342 $this->thumb_paths = $thumb_paths; 343 } 344 345 public function getFileSuffix() { 346 return $this->file_suffix; 347 } 348 349 public function act_internal() { 350 351 $zip = new ZipArchive; 352 if ($zip->open($this->getSourceFilepath()) !== true) { 353 // file is no zip or cannot be opened 354 return false; 355 } 356 357 // The media file exists and acts as a zip file! 358 359 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 360 foreach($this->thumb_paths as $thumbnail_path) { 361 $this->file_suffix = substr(strrchr($thumbnail_path,'.'),1); 362 363 if ($zip->locateName($thumbnail_path) !== false) { 364 365 if (!$this->thumb_needs_update()) { 366 return true; 367 } 368 369 // Get the thumbnail file! 370 $fp = $zip->getStream($thumbnail_path); 371 if(!$fp) { 372 return false; 373 } 374 375 $thumbnaildata = ''; 376 while (!feof($fp)) { 377 $thumbnaildata .= fread($fp, 8192); 378 } 379 380 fclose($fp); 381 382 // Write thumbnail file to media folder 383 file_put_contents($this->getTargetFilepath(), $thumbnaildata); 384 385 return true; 386 } 387 } 388 389 return true; 390 } 391}