-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvcode.php
86 lines (75 loc) · 2.85 KB
/
vcode.php
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
<?php
require_once('include/db_info.inc.php');
/****************************************
* 验证码 v0.9
* Powerd by awaysoft.com
* 本组件采用GPLv3发布
* 2011-07-15
****************************************/
/* $len 为随机字符串长度,$type为类型,a为字符数字,c为字符,n为数字, 已经去除可能误导的字符 */
function get_rand_string($len=4, $type="n")
{
if ($len < 0) {
$len = 4;
}
if ($type == 'a') {
$chars = 'ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz023456789';
} elseif ($type == 'c') {
$chars = 'ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz';
} elseif ($type == 'n') {
$chars = '0123456789';
} else {
$chars = '0123456789';
}
$result = '';
for ($i = 0; $i < $len; $i ++) {
$index = mt_rand(0, strlen($chars) - 1);
$result .= substr($chars, $index, 1);
}
return $result;
}
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pramga: no-cache");
/* 输出图片类型,字符长度,类型 */
$imgtype = 'gif';
$len = 4;
$vcodetype = 'n';
$width = 15 * $len;
$height = 24;
/* 生成随机字符串并写入SESSION */
$vcode = get_rand_string($len, $vcodetype);
$_SESSION[$OJ_NAME.'_'.'vcode'] = $vcode;
header("Content-type: image/".$imgtype);
if ($imgtype != 'gif' && function_exists('imagecreatetruecolor')) {
$im = imagecreatetruecolor($width, $height);
} else {
$im = imagecreate($width, $height);
}
$r = mt_rand(0, 255);
$g = mt_rand(0, 255);
$b = mt_rand(0, 255);
/* 生成背景颜色 */
$backColor = ImageColorAllocate($im, $r, $g, $b);
/* 生成边框颜色 */
$borderColor = ImageColorAllocate($im, 0, 0, 0);
/* 生成干扰点颜色 */
$pointColor = ImageColorAllocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
/* 背景位置 */
imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
/* 边框位置 */
imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
/* 字符串颜色(背景反色) */
$stringColor = ImageColorAllocate($im, 255 - $r, 255 - $g, 255 - $b);
/* 产生干扰点 */
$pointNumber = mt_rand($len * 25, $len * 50);
for ($i=0; $i<=$pointNumber; $i++) {
$pointX = mt_rand(2, $width-2);
$pointY = mt_rand(2, $height-2);
imagesetpixel($im, $pointX, $pointY, $pointColor);
}
imagettftext($im, 15, 0, 4, 20, $stringColor, "include/Vera.ttf", $vcode);
$image_out = 'Image' . $imgtype;
$image_out($im);
@ImageDestroy($im);