简单封装验证码

[广州PHP]简单封装验证码。

代码:
[PHP] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
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
<?php
class CaptchaTool{

private $_img;//保存画布资源的非静态属性
private $_w;//画布的宽度
private $_h;//画布的高度

#构造方法   初始化画布资源操作
public function __construct($w=200, $h=80){ 
     
    //创建画布
    $this->_w = $w;
    $this->_h = $h;
    $this->_img = imagecreatetruecolor($w, $h);

    //填充背景色
    $color = $this->getColor();//调用getColor方法获得随机颜色
    imagefill($this->_img, 0, 0, $color);
}

#分配颜色的方法
private function getColor($r='', $g='', $b=''){ 
     
    //  如果$r为空字符串,则说明没有传递参数,那么我们就给随机值;如果不全等于空字符串,则表示用户在调用时传递了指定的色值,那么就直接使用用户传递的色值即可
    $r = ($r==='') ? mt_rand(0, 255) : $r;
    $g = ($g==='') ? mt_rand(0, 255) : $g;
    $b = ($b==='') ? mt_rand(0, 255) : $b;

    //分配颜色
    return imagecolorallocate($this->_img, $r, $g, $b);
}

#构建随机字符写入画布

public function writeStr($num=4){

     
    $strArr = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9));//构建随机字库

    //生成指定个数的随机字符串
    $str = '';
    for($i=0; $i<$num; $i++ ){ 
         
        $key = mt_rand(0, count($strArr)-1);
        $str .= $strArr[$key];
    }

    //往画布上写字
    $x = $this->_w/5;//文字起始点的x坐标
    $y = $this->_h*2/3;//文字起始点的y坐标
    $color = $this->getColor();//分配一个随机色
    $size = $this->_w*3/20;//字体的大小为宽度的3/20
    imagettftext($this->_img, $size, 0, $x, $y, $color, 'H:/project/public/fonts/font1.ttf', $str);

    return $str;
}

#设置干扰点
public function setPoint($num=300){ 
     
    for($i=0; $i<$num; $i++ ){ 
        $bx = mt_rand(0, $this->_w);//起点的x坐标
        $by = mt_rand(0, $this->_h);//起点的y坐标
        $ex = mt_rand($bx-2, $bx+2);//终点的x坐标
        $ey = mt_rand($by-2, $by+2);//终点的y坐标

        $color = $this->getColor();//分配随机色

        imageline($this->_img, $bx, $by, $ex, $ey, $color);
    }
}

#设置干扰线
public function setLine($num=10){ 
     
    for($i=0; $i<$num; $i++ ){ 
        $bx = mt_rand(0, $this->_w/2);//起点的x坐标
        $by = mt_rand(0, $this->_h);//起点的y坐标
        $ex = mt_rand($this->_w/2, $this->_w);//终点的x坐标
        $ey = mt_rand(0, $this->_h);//终点的y坐标

        $color = $this->getColor();//分配随机色

        imageline($this->_img, $bx, $by, $ex, $ey, $color);
    }
}

#输出图像到浏览器

public function output(){ 

    header('Content-type:image/jpeg');
    imagejpeg($this->_img);
}

}

$obj = new CaptchaTool;
$obj->writeStr();
$obj->output();

效果:

图片描述
图片描述

评论

暂无评论