Chinaunix首页 | 论坛 | 博客
  • 博客访问: 97224
  • 博文数量: 75
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 747
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-11 14:42
个人简介

http://www.njtbo.com

文章分类

全部博文(75)

文章存档

2013年(75)

我的朋友

分类: IT职场

2013-03-14 09:45:24

验证码配色为统一颜色,只是透明不同,感觉这样更安全些。


02     /**
03     * @package 验证码类
04     * @version 1.1
05     *
06     * @link
07     *
08     */
09     class Captcha {
10     /**
11     * 检验验证码时注意:验证码采用下面方式保存
12     * SESSION中保存的值为: $_SESSION['Captcha'] = md5(strtoupper(验证码文本));
13     */
14     
15     private $_fontDir = '../font/ariali.ttf'; // 验证码字体 这个字体系统里都带着呐 自己去找找
16     private $_imgWidth = 120; // 背景宽度
17     private $_imgHeight = 40; // 背景高度
18     private $_imgText = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 验证码字符集
19     private $_imgTextLen = 4; // 验证码长度
20     private $_imgTextSP = 24; // 验证码字符间距
21     private $_imgTextT = 18; // 验证码最大字体
22     private $_imgTextB = 14; // 验证码最小字体
23     private $_imgTextRot = true; // 字体倾斜 treu为启用 false为关闭
24     private $_imgBGLine = 5; // 背景干扰线数量
25     private $_imgLine = 3; // 横向干扰线数量
26     private $_imgDot = 100; // 干扰点数量
27     function __construct()
28     {
29     // 定义PHP输出内容类型
30     header('Content-Type: image/png');
31     session_start();
32     // 新建真彩图
33     $img = imagecreatetruecolor($this->_imgWidth, $this->_imgHeight);
34     // 真彩图背景色
35     $imgColor = imagecolorallocate($img, 255, 255, 255);
36     // 填充真彩图
37     imagefill($img, 0, 0, $imgColor);
38     // 随机RGB值
39     $r = rand(50, 120);
40     $g = rand(50, 120);
41     $b = rand(50, 120);
42     // 干扰线颜色
43     $linecolor = imagecolorallocatealpha($img, $r, $g, $b, 85);
44     // 背景干扰线
45     for ($i=0; $i < $this->_imgBGLine; $i++) {
46     imagesetthickness($img, rand(8, 16));
47     imageline($img, rand(5, 60), rand(0, $this->_imgHeight), rand($this->_imgWidth-5, $this->_imgHeight-60), rand($this->_imgHeight, 0), $linecolor);
48     }
49     // 横向干扰线
50     for ($i=0; $i < $this->_imgLine; $i++) {
51     imagesetthickness($img, rand(4, 8));
52     imageline($img, 0, rand(0, $this->_imgHeight), $this->_imgWidth, rand($this->_imgHeight, 0), $linecolor);
53     }
54     // 干扰点
55     for ($i=0; $i < $this->_imgDot; $i++) {
56     $dotcolor = imagecolorallocatealpha($img, $r, $g, $b, rand(0,20));
57     imagesetpixel($img, rand(0, $this->_imgWidth), rand(0,$this->_imgHeight), $dotcolor);
58     }
59     // 文字颜色
60     $imgTextColor = imagecolorallocate($img, $r, $g, $b);
61     //session数组
62     $sessionvalue = array();
63     // 文字X轴起点
64     $textx = rand(2,20);
65     // 根据验证码长度循环写入字符
66     for ($i=0; $i < $this->_imgTextLen; $i++) {
67     // 取得验证码字符集的长度
68     $strlen = strlen($this->_imgText);
69     // 随机取一个验证码字符集内字符
70     $text = $this->_imgText[rand(0, $strlen-1)];
71     // 把取得字符添加到sessionvalue数组
72     $sessionvalue[$i] = $text;
73     if ( $this->_imgTextRot !== false ) {
74     $textrot = rand(-20, 20);
75     }else{
76     $textrot = 0;
77     }
78     imagettftext($img, rand($this->_imgTextB, $this->_imgTextT), $textrot, $textx+($i*$this->_imgTextSP), rand(20, $this->_imgHeight-4), $imgTextColor, $this->_fontDir, $text);
79     }
80     // 把sessionvalue数组内元素合并为字符串
81     $sessionText = implode('', $sessionvalue);
82     // 把字符串保存到$_SESSION['Captcha']中
83     $_SESSION['Captcha'] = md5(strtoupper($session)Text);
84     imagepng($img);
85     }
86     function __destruct ()
87     {
88     // 清除图像资源
89     imagedestroy($img);
90     }
91     }
92     $Captcha = new Captcha();
93     ?>

本文由整理转发


阅读(407) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~