1<?php 2/** LiveMark dokuwiki plugin 3 * 4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 5 * @author s c yoo <dryoo@live.com> 6 * url http://openwiki.kr/tech/livemark 7 * 8 * bASED ON iReflect Plugin https://github.com/i-net-software/dokuwiki-plugin-reflect 9 */ 10 11// must be run within Dokuwiki 12if(!defined('DOKU_INC')) die(); 13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 14require_once(DOKU_PLUGIN.'action.php'); 15 16class action_plugin_livemark extends DokuWiki_Action_Plugin { 17 18 function register(Doku_Event_Handler $controller) { 19 if ( isset($_REQUEST['i']) ) { return; } 20 $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'livemark__livemark'); 21 } 22 23 function livemark__livemark(&$event, $args) { 24 if (extension_loaded('gd') == false && !@dl('gd.so')) { return; } 25 $data = $event->data; 26 if ($data['ext']=='jpg' || $data['ext']=='png') 27 { 28 $ext=$data['ext']; 29 $cacheFile = getCacheName($data['file'],".media.watermark.$ext"); 30 $mtime = @filemtime($cacheFile); // 0 if not exists 31 $cache = $data['cache']; 32 33 if( ($mtime == 0) || // cache does not exist 34 ($mtime < time()-$cache) // 'recache' and cache has expired 35 ){ 36 if ( $this->create_watermark_image( $data, $cacheFile ) ) { 37 $data['orig'] = $data['file']; 38 $data['file'] = $cacheFile; 39 list($data['ext'],$data['mime'],$data['download']) = mimetype($cacheFile); 40 $event->data = $data; 41 } 42 } 43 } 44 } 45 46 function create_watermark_image( $data, $cache_path ) { 47 global $conf; 48 $input = $data['file']; 49 50 if ( !($image_details = getimagesize($input)) ) { return false; } 51 52 $width = $image_details[0]; 53 $height = $image_details[1]; 54 $type = $image_details[2]; 55 $mime = $image_details['mime']; 56 57 if ($width<$this->getConf('minsize')) return false; 58 // Detect the source image format 59 switch ($type) 60 { 61 case 1:// GIF 62 $source = imagecreatefromgif($input); break; 63 case 2:// JPG 64 $source = imagecreatefromjpeg($input); break; 65 case 3:// PNG 66 $source = imagecreatefrompng($input); break; 67 default: return false; 68 } 69 70 /* Build the watermark image */ 71 $output = $this->imagewatermark($source, $width, $height); 72 73 /* Output our final Image */ 74 if ( headers_sent() ) { return false; } 75 76 // If you'd rather output a JPEG instead of a PNG then pass the parameter 'jpeg' (no value needed) on the querystring 77 if ( substr($cache_path, -3) == 'png' ) { 78 imagepng($output, $cache_path, intval($conf['jpg_quality'] / 11)); 79 } else if ( substr($cache_path, -3) == 'jpg' ) { 80 $finaloutput = imagecreatetruecolor($width, $height); 81 imagecopy($finaloutput, $output, 0, 0, 0, 0, $width, $height); 82 imagejpeg($finaloutput, $cache_path, intval($conf['jpg_quality'])); 83 } 84 imagedestroy($output); 85 return true; 86 } 87 88 function imagewatermark($src_img, $src_width, $src_height) { 89 global $conf; 90 // Create Object 91 $marked = $src_img; 92 //$marked = imagecreatetruecolor($src_width, $src_height); 93 94 // Copy Source 95 //imagecopy($marked, $src_img, 0, 0, 0, 0, $src_width, $src_height); 96 imagealphablending($marked, true); 97 imagesavealpha($marked, true); 98 99 $watermark =imagecreatefrompng($this->getConf('watermark_path'));//'../plugins/livemark/livemark.png'); 100 101 $size=$this->getConf('size')/100; 102 $ratio=$src_width/imagesx($watermark)*$size; 103 $watermark_width = imagesx($watermark)*$ratio; 104 $watermark_height = imagesy($watermark)*$ratio; 105 $dest_x = ($src_width - $watermark_width)/2-$src_width*(0.5-$this->getConf('horizontal')/100); 106 $dest_y = ($src_height - $watermark_height)/2-$src_height*(0.5-$this->getConf('vertical')/100); 107 imagecopyresampled($marked, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, imagesx($watermark), imagesy($watermark)); 108 109 return $marked; 110 } 111} 112 113