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_width'); 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 124class thumbnail { 125 126 private $source_filepath; 127 private $source_mediapath; 128 private ?thumb_engine $thumb_engine = null; 129 130 public function __construct(string $source_filepath, DokuWiki_Syntax_Plugin $plugin, bool $ismediapath = true) { 131 132 if ($ismediapath) { 133 $this->source_mediapath = $source_filepath; 134 $this->source_filepath = mediaFN($source_filepath); 135 } else { 136 $this->source_mediapath = false; 137 $this->source_filepath = $source_filepath; 138 } 139 140 // Now attach the correct thumb_engine for the file type of the source file 141 //TODO: check for extension "fileinfo", then check for MIME type: if (mime_content_type($filepath_local_file) == "application/pdf") { 142 if (substr($this->source_filepath,-4) == ".pdf") { 143 $this->thumb_engine = new thumb_pdf_engine($this,$plugin->getConf('thumb_width')); 144 } else if (substr($this->source_filepath,-4) == ".jpg") { 145 $this->thumb_engine = new thumb_img_engine($this,$plugin->getConf('thumb_width')); 146 } else { 147 $this->thumb_engine = new thumb_zip_engine($this,$plugin->getConf('thumb_width'),$plugin->getConf('thumb_paths')); 148 } 149 } 150 151 public function create() { 152 if (!$this->thumb_engine) { 153 return false; 154 } 155 156 return $this->thumb_engine->act(); 157 } 158 159 public function getSourceFilepath() { 160 return $this->source_filepath; 161 } 162 163 protected function getFilename() { 164 165 return basename($this->source_filepath) . ".thumb.".$this->thumb_engine->getFileSuffix(); 166 } 167 168 public function getFilepath() { 169 return dirname($this->source_filepath) . DIRECTORY_SEPARATOR . $this->getFilename(); 170 } 171 172 public function getMediapath() { 173 if ($this->source_mediapath !== false) { 174 return substr($this->source_mediapath,0,strrpos($this->source_mediapath,':')) . ":" . $this->getFilename(); 175 } else { 176 return false; 177 } 178 } 179 180 public function getTimestamp() { 181 return file_exists($this->getFilepath()) ? filemtime($this->getFilepath()) : false; 182 } 183} 184 185abstract class thumb_engine { 186 187 private ?thumbnail $thumbnail = null; 188 private int $width; 189 190 public function __construct(thumbnail $thumbnail, int $width) { 191 $this->thumbnail = $thumbnail; 192 $this->width = $width; 193 } 194 195 protected function getSourceFilepath() { 196 return $this->thumbnail->getSourceFilepath(); 197 } 198 199 protected function getTargetFilepath() { 200 return $this->thumbnail->getFilepath(); 201 } 202 203 protected function getTargetWidth() { 204 return $this->width; 205 } 206 207 public function act() { 208 if ($this->act_internal()) { 209 // 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). 210 if (filemtime($this->getSourceFilepath()) !== filemtime($this->getTargetFilepath())) { 211 touch($this->getTargetFilepath(), filemtime($this->getSourceFilepath())); 212 } 213 return true; 214 } 215 return false; 216 } 217 218 // Checks if a thumbnail file for the current file version has already been created 219 protected function thumb_needs_update() { 220 return !file_exists($this->getTargetFilepath()) || filemtime($this->getTargetFilepath()) !== filemtime($this->getSourceFilepath()); 221 } 222 223 public abstract function act_internal(); 224 225 public abstract function getFileSuffix(); 226} 227 228class thumb_pdf_engine extends thumb_engine { 229 230 public function getFileSuffix() { 231 return "jpg"; 232 } 233 234 public function act_internal() { 235 if ($this->thumb_needs_update()) { 236 $im = new imagick( $this->getSourceFilepath()."[0]" ); 237 $im->setImageColorspace(255); 238 $im->setResolution(300, 300); 239 $im->setCompressionQuality(95); 240 $im->setImageFormat('jpeg'); 241 //$im->resizeImage(substr($this->getConf('thumb_width'),-2),0,imagick::FILTER_LANCZOS,0.9); 242 $im->writeImage($this->getTargetFilepath()); 243 $im->clear(); 244 $im->destroy(); 245 246 return true; 247 } else { 248 return true; 249 } 250 } 251} 252 253class thumb_img_engine extends thumb_engine { 254 255 public function getFileSuffix() { 256 $this->file_suffix = substr(strrchr($this->getSourceFilepath(),'.'),1); 257 return $this->file_suffix; 258 } 259 260 public function act_internal() { 261 if ($this->thumb_needs_update()) { 262 $im = new imagick( $this->getSourceFilepath() ); 263 //TODO: consider height? 264 $im->thumbnailImage($this->getTargetWidth(),$this->getTargetWidth(),true,false); 265 $im->writeImage($this->getTargetFilepath()); 266 $im->clear(); 267 $im->destroy(); 268 269 return true; 270 } else { 271 return true; 272 } 273 } 274} 275 276class thumb_zip_engine extends thumb_engine { 277 278 private array $thumb_paths; 279 private $file_suffix = ""; 280 281 public function __construct(thumbnail $thumbnail, int $width, array $thumb_paths) { 282 parent::__construct($thumbnail,$width); 283 $this->thumb_paths = $thumb_paths; 284 } 285 286 public function getFileSuffix() { 287 return $this->file_suffix; 288 } 289 290 public function act_internal() { 291 292 $zip = new ZipArchive; 293 if ($zip->open($this->getSourceFilepath()) !== true) { 294 // file is no zip or cannot be opened 295 return false; 296 } 297 298 // The media file exists and acts as a zip file! 299 300 // Check all possible paths (configured in configuration key 'thumb_paths') if there is a file available 301 foreach($this->thumb_paths as $thumbnail_path) { 302 $this->file_suffix = substr(strrchr($thumbnail_path,'.'),1); 303 304 if ($zip->locateName($thumbnail_path) !== false) { 305 306 if (!$this->thumb_needs_update()) { 307 return true; 308 } 309 310 // Get the thumbnail file! 311 $fp = $zip->getStream($thumbnail_path); 312 if(!$fp) { 313 return false; 314 } 315 316 $thumbnaildata = ''; 317 while (!feof($fp)) { 318 $thumbnaildata .= fread($fp, 8192); 319 } 320 321 fclose($fp); 322 323 // Write thumbnail file to media folder 324 file_put_contents($this->getTargetFilepath(), $thumbnaildata); 325 326 return true; 327 } 328 } 329 330 return true; 331 } 332}