php 生成验证码类

<?php
namespace Mylib;
/**
 * 该类实例化的时候需要3个参数
 * $type;//验证码1为数字 2为字母 3为数字加字母
 * $width;//验证码的宽,默认值为80px
 * $height;//验证码的高,默认值为20px
 * $num;//验证码字符的个数,默认值为4
 * $create_code;//验证码字符,可以为空
 */
/*
    show_image_code();echo 图片,
    get_code();获取code,
*/
class Code{
    private $width;//验证码的宽
    private $height;//验证码的高
    private $num;//验证码字符的个数
    private $code;//验证码的字符串
    private $img;//验证码source
    function __construct($type = 3,$width=80,$height=30,$num=4,$create_code=''){
        $this->width=$width;
        $this->height=$height;
        $this->num=$num;
        empty($create_code) &&  $create_code=$this->create_code($type);
        $this->code=$create_code;
    }
    private function create_canvas(){//创建画布
        $this->img=imagecreatetruecolor($this->width,$this->height);
        $background_color=imagecolorallocate($this->img,0xFF,0xFF,0xFF);
        imagefill($this->img,0,0,$background_color);
        $border_color=imagecolorallocate($this->img,0xAA,0xAA,0xAA);
        imagerectangle($this->img,0,0,$this->width-1,$this->height-1,$border_color);
    }
    private function create_code($type){//生成验证码的字符串
        //1为数字 2为字母 3为数字加字母
        if ($type == 1) {
            $src="0123456789";
        }elseif ($type == 2) {
            $src="qwertyupkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM";
        }else{
            $src="0123456789qwertyupkjhgfdsazxcvbnmQWERTYUPLKJHGFDSAZXCVBNM";
        }
        $code="";
        for($i=0;$i<$this->num;$i++){
            $index=mt_rand(0,strlen($src)-1);
            $code.=$src[$index];
        }
        return $code;
    }
    private function paint_char(){//将生成的字符串画在画布上
        for($i=0;$i<$this->num;$i++){
            $char_color=imagecolorallocate($this->img,0xFF,0,0xFF);
            $font_size=4;
            $x=5+($this->width/$this->num)*$i;
            $y=($this->height-imagefontheight($font_size))/2;

            imagechar($this->img,$font_size,$x,$y,$this->code[$i],$char_color);
        }
    }
    private function add_disturbance(){//添加干扰标记
        for($i=0;$i<20;$i++){
            $color=imagecolorallocate($this->img,rand(0,255),rand(0,255),rand(0,255));
            imagesetpixel($this->img,rand(1,$this->width-2),rand(1,$this->height-2),$color);
        }
    }
    private function print_code() {//判断兼容哪种格式
        if (imagetypes() & IMG_PNG) {
            header("Content-type: image/png");
            imagepng($this->img);
        } elseif (imagetypes() & IMG_JPG) {
            header("Content-type: image/jpeg");
            imagejpeg($this->img);
        }  else {
              die("No image support in this PHP server");
        }
    }
    private function destroy_code(){//释放资源
        imagedestroy($this->img);
    }

    public function get_code(){//获取验证码字符串的值
        return strtolower($this->code);
    }
    
    public function show_image_code(){//所有步骤的汇集,搞定所有验证码的工作
        $this->create_canvas();
        $this->paint_char();
        $this->add_disturbance();
        $this->print_code();
        $this->destroy_code();
    }

}



?>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值