1<?php 2/** 3 * DokuWiki Plugin watermark (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Christian Moll <christian@chrmoll.de> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 11 12// must be run within Dokuwiki 13if(!defined('DOKU_INC')) die(); 14 15class action_plugin_watermark_watermark extends DokuWiki_Action_Plugin { 16 17 /** 18 * Registers a callback function for a given event 19 * 20 * @param Doku_Event_Handler $controller DokuWiki's event controller object 21 * @return void 22 */ 23 public function register(Doku_Event_Handler $controller) { 24 25 $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload_finish'); 26 27 } 28 29 /** 30 * [Custom event handler which performs action] 31 * 32 * @param Doku_Event $event event object by reference 33 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 34 * handler was registered] 35 * @return void 36 */ 37 38 public function handle_media_upload_finish(Doku_Event &$event, $param) { 39 // get watermark either out of the template images folder or data/media folder 40 $watermarkSize = array(); 41 $watermarkFile = tpl_getMediaFile(array(':wiki:watermark.png', ':watermark.png'), true, $watermarkSize); 42 $watermark = imagecreatefrompng(str_replace(':/','://',str_replace('//','/', $watermarkFile))); 43 44 $sourcefile = $event->data[1]; 45 $fileType = strtolower(substr($sourcefile, strlen($sourcefile)-3)); 46 $isWatermark = strpos($sourcefile, 'watermark.png'); 47 48 if ($isWatermark === false ) { 49 switch($fileType) { 50 case('gif'): 51 $image = imagecreatefromgif($sourcefile); 52 break; 53 54 case('png'): 55 $image = imagecreatefrompng($sourcefile); 56 break; 57 58 default: 59 $image = imagecreatefromjpeg($sourcefile); 60 } 61 62 $watermark_width = imagesx($watermark); 63 $watermark_height = imagesy($watermark); 64 65 $size = getimagesize($sourcefile); 66 67 $dest_x = $size[0] - $watermark_width - 5; 68 $dest_y = $size[1] - $watermark_height - 5; 69 70 imagesavealpha($image, true); 71 imagealphablending($image, true); 72 imagesavealpha($watermark, true); 73 imagealphablending($watermark, true); 74 75 imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height); 76 77 switch($fileType) { 78 79 // remember we don't need gif any more, so we use only png or jpeg. 80 // See the upsaple code immediately above to see how we handle gifs 81 case('png'): 82 imagepng($image, $sourcefile, 0); 83 break; 84 default: 85 imagejpeg($image, $sourcefile, 90); 86 } 87 88 89 imagedestroy($image); 90 imagedestroy($watermark); 91 } 92 } 93} 94 95// vim:ts=4:sw=4:et: 96