PluginProbe
WPWaterMark 轻水印插件 / 4.3
WPWaterMark 轻水印插件 v4.3
5.2.4 5.2.2 5.2.1 5.1.7 5.1.6 trunk 4.3 5.0.0 5.0.1 5.1.2 5.1.3 5.1.4 5.1.5
wpwatermark / WaterMarkFunctions.php

WaterMarkFunctions.php in WPWaterMark 轻水印插件 4.3, at WaterMarkFunctions.php

261 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 function wpWaterMarkPosition($point, $imgWidth, $imgHeight, $textWidth, $textLength, $lineHeight, $margin) {
3 $pointLeft = $margin;
4 $pointTop = $margin;
5 switch ($point) {
6 case 1:
7 $pointLeft = $margin;
8 $pointTop = $margin;
9 break;
10 case 2:
11 $pointLeft = floor(($imgWidth - $textWidth) / 2);
12 $pointTop = $margin;
13 break;
14 case 3:
15 $pointLeft = $imgWidth - $textWidth - $margin;
16 $pointTop = $margin;
17 break;
18 case 4:
19 $pointLeft = $margin;
20 $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
21 break;
22 case 5:
23 $pointLeft = floor(($imgWidth - $textWidth) / 2);
24 $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
25 break;
26 case 6:
27 $pointLeft = $imgWidth - $textWidth - $margin;
28 $pointTop = floor(($imgHeight - $textLength * $lineHeight) / 2);
29 break;
30 case 7:
31 $pointLeft = $margin;
32 $pointTop = $imgHeight - $textLength * $lineHeight - $margin;
33 break;
34 case 8:
35 $pointLeft = floor(($imgWidth - $textWidth) / 2);
36 $pointTop = $imgHeight - $textLength * $lineHeight - $margin;
37 break;
38 case 9:
39 $pointLeft = $imgWidth - $textWidth - $margin;
40 $pointTop = $imgHeight - $textLength * $lineHeight - $margin;
41 break;
42 }
43 return array('pointLeft' => $pointLeft, 'pointTop' => $pointTop);
44 }
45 function wpWaterMarkSetAngle ( $angle, $point, $position, $textWidth, $imgHeight ) {
46 if ($angle < 90) {
47 $diffTop = ceil(sin(deg2rad($angle)) * $textWidth);
48
49 if (in_array($point, array(1, 2, 3))) {
50 $position['pointTop'] += $diffTop;
51 } elseif (in_array($point, array(4, 5, 6))) {
52 if ($textWidth > ceil($imgHeight / 2)) {
53 $position['pointTop'] += ceil(($textWidth - $imgHeight / 2) / 2);
54 }
55 }
56 } elseif ($angle > 270) {
57 $diffTop = ceil(sin(deg2rad(360 - $angle)) * $textWidth);
58
59 if (in_array($point, array(7, 8, 9))) {
60 $position['pointTop'] -= $diffTop;
61 } elseif (in_array($point, array(4, 5, 6))) {
62 if ($textWidth > ceil($imgHeight / 2)) {
63 $position['pointTop'] = ceil(($imgHeight - $diffTop) / 2);
64 }
65 }
66 }
67 return $position;
68 }
69 function wpwatermark_hex2rgb($hexColor) {
70 $color = str_replace('#', '', $hexColor);
71 if (strlen($color) > 3) {
72 $rgb = array(
73 'r' => hexdec(substr($color, 0, 2)),
74 'g' => hexdec(substr($color, 2, 2)),
75 'b' => hexdec(substr($color, 4, 2))
76 );
77 } else {
78 $color = $hexColor;
79 $r = substr($color, 0, 1) . substr($color, 0, 1);
80 $g = substr($color, 1, 1) . substr($color, 1, 1);
81 $b = substr($color, 2, 1) . substr($color, 2, 1);
82 $rgb = array(
83 'r' => hexdec($r),
84 'g' => hexdec($g),
85 'b' => hexdec($b)
86 );
87 }
88 return $rgb['r'].','.$rgb['g'].','.$rgb['b'];
89 }
90 function wpWaterMarkCreateWordsWatermark($imgurl, $newimgurl, $text, $margin='30', $fontSize = '14', $color = '#790000', $point = '1', $font = 'simhei.ttf', $angle = '0', $watermark_margin = '80' )
91 {
92 $margin = intval($margin);
93 $angle = intval($angle);
94 $watermark_margin = intval($watermark_margin);
95 $imageCreateFunArr = array(
96 'image/jpeg' => 'imagecreatefromjpeg', 'image/png' => 'imagecreatefrompng', 'image/gif' => 'imagecreatefromgif'
97 );
98 $imageOutputFunArr = array('image/jpeg' => 'imagejpeg', 'image/png' => 'imagepng', 'image/gif' => 'imagegif');
99 $imgsize = getimagesize($imgurl);
100 if (empty($imgsize)) { return false; }
101 $imgWidth = $imgsize[0];
102 $imgHeight = $imgsize[1];
103 $imgMime = $imgsize['mime'];
104 if (!isset($imageCreateFunArr[$imgMime])) { return false; }
105 if (!isset($imageOutputFunArr[$imgMime])) { return false; }
106 $imageCreateFun = $imageCreateFunArr[$imgMime];
107 $imageOutputFun = $imageOutputFunArr[$imgMime];
108 $im = $imageCreateFun($imgurl);
109 $color = explode(',', wpwatermark_hex2rgb($color));
110 $text_color = imagecolorallocate($im, intval($color[0]), intval($color[1]), intval($color[2]));
111 $point = intval($point) >= 0 && intval($point) <= 10 ? intval($point) : 1;
112 $fontSize = intval($fontSize) > 0 ? intval($fontSize) : 14;
113 $angle = ($angle >= 0 && $angle < 90 || $angle > 270 && $angle < 360) ? $angle : 0;
114 $fontUrl = plugin_dir_path( __FILE__ ) . 'fonts/' . ($font ? $font : 'alibaba.otf');
115 $text = explode('|', $text);
116 $textLength = count($text) - 1;
117 $maxtext = 0;
118 foreach ($text as $val) {
119 $maxtext = strlen($val) > strlen($maxtext) ? $val : $maxtext;
120 }
121 $textSize = imagettfbbox($fontSize, 0, $fontUrl, $maxtext);
122 $textWidth = $textSize[2] - $textSize[1];
123 $textHeight = $textSize[1] - $textSize[7];
124 $lineHeight = $textHeight + 3;
125 if ($textWidth + 40 > $imgWidth || $lineHeight * $textLength + 40 > $imgHeight) { return false; }
126 if ($point == 10) {
127 $position = array('pointLeft' => $margin, 'pointTop' => $margin);
128 if ($angle != 0) {
129 $position = wpWaterMarkSetAngle($angle, $point, $position, $textWidth, $imgHeight);
130 }
131 $x_length = $imgWidth - $margin;
132 $y_length = $imgHeight - $margin;
133 for ($x = $position['pointLeft']; $x < $x_length; $x++ ) {
134 for ($y = $position['pointTop']; $y < $y_length; $y++) {
135 foreach ($text as $key => $val) {
136 imagettftext($im, $fontSize, $angle, $x, $y + $key * $lineHeight, $text_color, $fontUrl, $val);
137 }
138 $y += ($lineHeight * count($text) + $watermark_margin);
139 }
140 $x += ($textWidth + $watermark_margin);
141 }
142 } else {
143 if ( $point == 0 ) {
144 $point = mt_rand(1, 9);
145 }
146 $position = wpWaterMarkPosition($point, $imgWidth, $imgHeight, $textWidth, $textLength, $lineHeight, $margin);
147 if ($angle != 0) {
148 $position = wpWaterMarkSetAngle($angle, $point, $position, $textWidth, $imgHeight);
149 }
150 foreach ($text as $key => $val) {
151 imagettftext($im, $fontSize, $angle, $position['pointLeft'], $position['pointTop'] + $key * $lineHeight, $text_color, $fontUrl, $val);
152 }
153 }
154 if ( $imgMime == 'image/jpeg' ) {
155 $imageOutputFun($im, $newimgurl, 100);
156 } else {
157 $imageOutputFun($im, $newimgurl);
158 }
159 imagedestroy($im);
160 return $newimgurl;
161 }
162 function wpWaterMarkImageWatermarkPosition( $point, $imgWidth, $imgHeight, $stampWidth, $stampHeight, $margin )
163 {
164 $pointLeft = $margin;
165 $pointTop = $margin;
166 if ( $point == 0 ) {
167 $point = mt_rand(1, 9);
168 }
169 switch ( $point ) {
170 case 1:
171 $pointLeft = $margin;
172 $pointTop = $margin;
173 break;
174 case 2:
175 $pointLeft = floor(($imgWidth - $stampWidth) / 2);
176 $pointTop = $margin;
177 break;
178 case 3:
179 $pointLeft = $imgWidth - $stampWidth - $margin;
180 $pointTop = $margin;
181 break;
182 case 4:
183 $pointLeft = $margin;
184 $pointTop = floor(($imgHeight - $stampHeight) / 2);
185 break;
186 case 5:
187 $pointLeft = floor(($imgWidth - $stampWidth) / 2);
188 $pointTop = floor(($imgHeight - $stampHeight) / 2);
189 break;
190 case 6:
191 $pointLeft = $imgWidth - $stampWidth - $margin;
192 $pointTop = floor(($imgHeight - $stampHeight) / 2);
193 break;
194 case 7:
195 $pointLeft = $margin;
196 $pointTop = $imgHeight - $stampHeight - $margin;
197 break;
198 case 8:
199 $pointLeft = floor(($imgWidth - $stampWidth) / 2);
200 $pointTop = $imgHeight - $stampHeight - $margin;
201 break;
202 case 9:
203 $pointLeft = $imgWidth - $stampWidth - $margin;
204 $pointTop = $imgHeight - $stampHeight - $margin;
205 break;
206 }
207 return array('pointLeft' => $pointLeft, 'pointTop' => $pointTop);
208 }
209 function wpWaterMarkCreateImageWatermark( $img_url, $stamp_url, $newimgurl, $point, $pct='100', $margin='30', $watermark_margin = '80' )
210 {
211 $pct = intval($pct);
212 $margin = intval($margin);
213 $watermark_margin = intval($watermark_margin);
214 $imageCreateFunArr = array(
215 'image/jpeg' => 'imagecreatefromjpeg', 'image/png' => 'imagecreatefrompng', 'image/gif' => 'imagecreatefromgif'
216 );
217 $imageOutputFunArr = array('image/jpeg' => 'imagejpeg', 'image/png' => 'imagepng', 'image/gif' => 'imagegif');
218 $im_size = getimagesize($img_url);
219 $stamp_size = getimagesize($stamp_url);
220 if ( empty($im_size) or empty($stamp_size) ) {return false;}
221 $imWidth = $im_size[0];
222 $imHeight = $im_size[1];
223 $imMime = $im_size['mime'];
224 $stampWidth = $stamp_size[0];
225 $stampHeight = $stamp_size[1];
226 $stampMime = $stamp_size['mime'];
227 if ( $imWidth < $stampWidth or $imHeight < $stampHeight ) {return false;}
228 if ( !isset($imageCreateFunArr[$imMime]) or !isset($imageCreateFunArr[$stampMime]) ) {return false;}
229 if ( !isset($imageOutputFunArr[$imMime]) ) {return false;}
230 $imCreateFun = $imageCreateFunArr[$imMime];
231 $imOutputFun = $imageOutputFunArr[$imMime];
232 $stampCreateFun = $imageCreateFunArr[$stampMime];
233 $im = $imCreateFun($img_url);
234 $stamp = $stampCreateFun($stamp_url);
235 $tcStamp = imagecreatetruecolor($stampWidth, $stampHeight);
236 $point = intval($point) >= 0 && intval($point) <= 10 ? intval($point) : 1;
237 if ( $point == 10 ) {
238 $x_length = $imWidth - $margin;
239 $y_length = $imHeight - $margin;
240 for ($x = $margin; $x < $x_length; $x++ ) {
241 for ($y = $margin; $y < $y_length; $y++) {
242 imagecopy($tcStamp, $im, 0, 0, $x, $y, $stampWidth, $stampHeight);
243 imagecopy($tcStamp, $stamp, 0, 0, 0, 0, $stampWidth, $stampHeight);
244 imagecopymerge($im, $tcStamp, $x, $y, 0, 0, $stampWidth, $stampHeight, $pct);
245 $y += ($stampHeight + $watermark_margin);
246 }
247 $x += ($stampWidth + $watermark_margin);
248 }
249 } else {
250 $position = wpWaterMarkImageWatermarkPosition($point, $imWidth, $imHeight, $stampWidth, $stampHeight, $margin);
251 imagecopy($tcStamp, $im, 0, 0, $position['pointLeft'], $position['pointTop'], $stampWidth, $stampHeight);
252 imagecopy($tcStamp, $stamp, 0, 0, 0, 0, $stampWidth, $stampHeight);
253 imagecopymerge($im, $tcStamp, $position['pointLeft'], $position['pointTop'], 0, 0, $stampWidth, $stampHeight, $pct);
254 }
255 if ( $imMime == 'image/jpeg' ) { $imOutputFun($im, $newimgurl, 100); } else { $imOutputFun($im, $newimgurl); }
256 imagedestroy($im);
257 imagedestroy($stamp);
258 imagedestroy($tcStamp);
259 return $newimgurl;
260 }
261