-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhua.php
More file actions
39 lines (38 loc) · 1.28 KB
/
hua.php
File metadata and controls
39 lines (38 loc) · 1.28 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
<?php
//定义画布
$width=300;
$height=50;
$image=imagecreatetruecolor($width,$height);
//随机颜色方法
function getRandColor($image){
return imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
}
//填充画布颜色为白色
$white=imagecolorallocate($image,255,255,255);
imagefilledrectangle($image,0,0,400,50,$white);
//创建出验证码库:0-9 a-z A-Z
$string=join('',array_merge(range(0,9),range('a','z'),range('A','Z')));
$length=strlen($string)-1;
//得出4位随机验证码
for($i=0;$i<4;$i++){
$randColor=getRandColor($image);
$size=mt_rand(20,28);
$angle=mt_rand(-15,15);
$x=20+60*$i; $y=30;
$fontFile='D:\PHPSTUDY\liuyan\fonts\arial.ttf';
$start=mt_rand(0,$length);
$text=substr($string,$start,1);
imagettftext($image,$size,$angle,$x,$y,$randColor,$fontFile,$text);}
//添加干扰元素
//50个随机像素点
for($i=1;$i<=50;$i++){
imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),getRandColor($image));
}
//绘制线段作于干扰元素
for($i=1;$i<=3;$i++){
imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),getRandColor($image));
}
//表现在画布上
header('content-type:image/png');
imagepng($image);
imagedestroy($image);