-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageClass.php
More file actions
270 lines (231 loc) · 7.87 KB
/
imageClass.php
File metadata and controls
270 lines (231 loc) · 7.87 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* 图片处理类
* @authors Andy Chen (bootoo@sina.cn)
* @date 2014-03-12 21:16:12
* @version $Id$
*/
class imageClass {
function __construct(){
$this->fontfile = 'XHei.ttc';
}
/**
* 验证码图片生成方法
* @param int $speck_num 画多少个点
* @param int $line_num 画多少条线
* @param int $font_num 填充文字数量
* @return img $im
*/
public function verification($speck_num,$line_num,$font_num){
$_SESSION['ver_code'] = '';
$im = imagecreatetruecolor(100, 30); //画布
$text = '23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ'; //填充内容(已经去除10lo)
// 随机背景颜色
$red = mt_rand(110,130);
$green = mt_rand(110,130);
$blue = mt_rand(110,130);
$back_color = imagecolorallocate($im, $red, $green, $blue); //背景颜色
$border_color = imagecolorallocate($im, 0, 114, 198); //边框颜色
imagefilledrectangle($im,0,0,100,30,$back_color); //画背景
imagerectangle($im, 0, 0, 100, 30, $border_color); //边框
// 随机生成点
for ($i=0; $i < $speck_num; $i++) {
$red = mt_rand(0,255);
$green = mt_rand(0,255);
$blue = mt_rand(0,255);
$x = mt_rand(0,100);
$y = mt_rand(0,30);
$speck_color = imagecolorallocate($im, $red, $green, $blue); //点(speck:杂点,污点)颜色
imagesetpixel($im, $x, $y, $speck_color); //画点
}
// 随机生成线
for ($i=0; $i < $line_num; $i++) {
$red = mt_rand(0,255);
$green = mt_rand(0,255);
$blue = mt_rand(0,255);
$cx = mt_rand(0,100);
$cy = mt_rand(0,30);
$width = mt_rand(0,90);
$height = mt_rand(0,90);
$start = mt_rand(0,360);
$end = mt_rand(0,360);
$line_color = imagecolorallocate($im, $red, $green, $blue); //线条颜色
imagearc($im, $cx, $cy, $width, $height, $start, $end, $line_color); //画线
}
// 随机生成文字
$fonts = '';
for ($i=1; $i <= $font_num; $i++) {
$red = mt_rand(0,255);
$green = mt_rand(0,255);
$blue = mt_rand(0,255);
$size = mt_rand(20,26);
$angle = mt_rand(0,26);
$x = 15*$i;
$y = mt_rand(25,30);
$font = '';
$start = mt_rand(0,(strlen($text)-1));
$font = substr($text, $start,1);
$font_color = imagecolorallocate($im, 0, 0, 0); //字体颜色
imagefttext($im, $size, $angle, $x, $y, $font_color, $this->fontfile, $font); //填充文字
$fonts .= $font;
}
$_SESSION['ver_code'] = $fonts;
return $im;
}
/**
* 图片缩放方法
* @param str $file 图片路径
* @param str $newFile 要生成的新文件名和路径
* @param int $width 要缩放的宽度
* @param int $height 要缩放的高度
* @return str $newFile 缩放后的图片路径
*/
public function thumb($file,$newFile,$width,$height){
$old_img = imagecreatefromjpeg($file);
// 得到图片的原宽高
$old_width = imagesx($old_img);
$old_height = imagesy($old_img);
// 原图比例
$ratio = $old_width/$old_height;
// 宽够,高够
if ($old_width >= $width and $old_height >= $height) {
if ($width > $height) {
$new_width = $width;
$new_height = $width/$ratio;
} elseif($width < $height) {
$new_width = $height*$ratio;
$new_height = $height;
}else{
if ($ratio >= 1) {
$new_width = $height*$ratio;
$new_height = $height;
} else {
$new_width = $width;
$new_height = $width/$ratio;
}
}
if ($ratio >= 1) {
$nx = -(($new_width/2) - ($width/2));
$ny = 0;
} else {
$nx = ($width/2) - ($new_width/2);
$ny = -(($new_height/2) - ($height/2));
}
}
// 宽够,高不够
if ($old_width >= $width and $old_height < $height) {
$new_width = $width;
$new_height = $width/$ratio;
$nx = 0;
$ny = ($height/2) - ($new_height/2);
}
// 宽不够,高够
if ($old_width < $width and $old_height >= $height) {
$new_width = $height*$ratio;
$new_height = $height;
$nx = ($width/2) - ($new_width/2);
$ny = 0;
}
// 宽不够,高不够
if ($old_width < $width and $old_height < $height) {
$new_width = $old_width;
$new_height = $old_height;
$nx = ($width/2) - ($new_width/2);
$ny = ($height/2) - ($new_height/2);
}
//原图坐标
$ox = 0;
$oy = 0;
//生成新图片
$new_img = imagecreatetruecolor($width,$height);
$back_color = imagecolorallocate($new_img, 255, 255, 255); //新图片背景颜色
imagefilledrectangle($new_img,0,0,$width,$height,$back_color); //新图片背景
//创建新的图像对象(缩略图)
imagecopyresized($new_img,$old_img,$nx,$ny,$ox,$oy, $new_width,$new_height,$old_width,$old_height);
// 生成图片
imagejpeg($new_img,$newFile);
// 销毁图片
imagedestroy($new_img);
return $newFile;
}
/**
* 图片加水印方法
* @param arr $img_path 原图信息
* @param str/arr $wt_con 水印内容
* @param str $seat 水印位置 1.up_l(上左) 2.up_r(上右) 3.do_l(下左) 4.do_r(下右)
* @param arr $gap 边距 (width=>侧边距离,height=>底部距离/顶部距离)
* @param int $alpha 透明度
*/
public function waterMark($img_path,$wt_con,$seat,$gap,$alpha=''){
$old_path = $img_path;
// 得到原图
$old_img = imagecreatefromjpeg($old_path);
$old_info = getimagesize($old_path);
// 得到水印信息(区分图片或文字)
if (is_array($wt_con)) {
$font = imagettfbbox($wt_con['size'], 0, $this->fontfile, $wt_con['text']);
$wt_info['0'] = $font[4] - $font[6];
$wt_info['1'] = $font[1] - $font[7];
} else {
$wt_img = imagecreatefrompng($wt_con);
$wt_info = getimagesize($wt_con);
}
// 计算水印位置
$water_seat = $this->wmGap($old_info,$wt_info,$seat,$gap);
// 打水印(区分图片或文字)
if (is_array($wt_con)) {
$font_color = imagecolorallocate($old_img, 234, 234, 234);
// 下面的$water_seat['gap_h']+20 因 imagettfbbox() 坐标系统和其他的统一所致
imagettftext($old_img, $wt_con['size'], 0, $water_seat['gap_w'], $water_seat['gap_h']+20, $font_color, $this->fontfile, $wt_con['text']);
} else {
if ($alpha == '') {
imagecopy($old_img, $wt_img, $water_seat['gap_w'], $water_seat['gap_h'], 0, 0, $wt_info['0'], $wt_info['1']); //无背景,不可透明
} else {
imagecopymerge($old_img, $wt_img, $water_seat['gap_w'], $water_seat['gap_h'], 0, 0, $wt_info['0'], $wt_info['1'], $alpha); //有背景,可透明
}
}
// 生成加水印后的图片
imagepng($old_img,$old_path);
// 销毁图片
imagedestroy($old_img);
imagedestroy($wt_img);
}
/**
* 计算水印位置
* @param arr $old_info 原图信息
* @param arr $wt_info 水印图信息
* @param str $seat 位置
* @param int $gap 边距
* @return arr $arr 处理好的数组
*/
public function wmGap($old_info,$wt_info,$seat,$gap){
switch ($seat) {
case 'up_l':
$arr['gap_w'] = $gap['0'];
$arr['gap_h'] = $gap['1'];
break;
case 'up_r':
$width = $old_info['0'] - $wt_info['0'] - $gap['0'];
$arr['gap_w'] = $width;
$arr['gap_h'] = $gap['1'];
break;
case 'do_l':
$height = $old_info['1'] - $wt_info['1'] - $gap['1'];
$arr['gap_w'] = $gap['0'];
$arr['gap_h'] = $height;
break;
case 'do_r':
$width = $old_info['0'] - $wt_info['0'] - $gap['0'];
$height = $old_info['1'] - $wt_info['1'] - $gap['1'];
$arr['gap_w'] = $width;
$arr['gap_h'] = $height;
break;
default:
$arr['gap_w'] = $gap['0'];
$arr['gap_h'] = $gap['1'];
break;
}
return $arr;
}
}
?>