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