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