php使用GD库生成验证码

  1. 目录

  2. 绘图步骤
  3. 绘制验证码步骤
  4. 代码演示

一、绘图步骤:
1.创建画布,分配颜色,使用以下两个函数(可以在php手册GD库函数中找到):
imagecreatetruecolor()
imagecolorallocate()

2.绘画过程:
imagefill()
imagettftext()
imagesetpixel()
imageline()
3.输出图像:
header(“Content-Type:image/png”);
imagepng();
imagejpeg();
4.销毁图片(释放内存)
imagedestroy();

二、绘图具体步骤
  1. 生成验证码字符串

private function getCode(){
        $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $t='';
        for($i=0;$i<$this->m;$i++){
            $t.=$str[rand(0,$this->type['letter'])];
        }
        return $t;
    }
  • 画出验证码
  • function drawCode(){
            $code=$this->getCode();//获取验证码字符串
            imagefill($this->im, 0, 0, $this->bg);
            for ($i=0; $i <200 ; $i++) { 
                imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255));
            }
            imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255));
            $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200));
            for($i=0;$i<$this->m;$i++){
                imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] );
            }
        }
  • 输出图像
  • function printImage(){
            $this->drawCode();
            header("Content-Type:image/jpeg");
            imagepng($this->im);
            $this->destroy();
        }
        function destroy(){
            imagedestroy($this->im);
        }

    三、代码演示

    <?php
    /**
    * 验证码画图类
    */
    class Image 
    {
        private $width;
        private $height;
        private $im;
        private $bg;
        private $m;
        private $type=array('num' => 9,'letter' => 61 );
        function __construct($width,$height,$m)
        {
            $this->width=$width;
            $this->height=$height;
            $this->m=$m;
            $this->im=imagecreatetruecolor($this->width, $this->height);
            $this->bg=imagecolorallocate($this->im, 220, 220, 220);
        }
        private function getCode(){
            $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            $t='';
            for($i=0;$i<$this->m;$i++){
                $t.=$str[rand(0,$this->type['letter'])];
            }
            return $t;
        }
        function drawCode(){
            $code=$this->getCode();//获取验证码字符串
            imagefill($this->im, 0, 0, $this->bg);
            //加干扰点
            for ($i=0; $i <200 ; $i++) { 
                imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255));
            }
            //加干扰线
            imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255));
            $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200));
            for($i=0;$i<$this->m;$i++){
                imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] );
            }
        }
        function printImage(){
            $this->drawCode();
            header("Content-Type:image/jpeg");
            imagepng($this->im);
            $this->destroy();
        }
        function destroy(){
            imagedestroy($this->im);
        }
    }
    
    function unit_test(){
        $obj=new Image(20*4,30,4);
        $obj->printImage();
    }
    
    unit_test();
    ?>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值