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