封装验证码类

<?php
/*
 * 封装验证码类
 */
class Captcha
{
    //成员属性
    private $width = 100;   //画布的宽度
    private $height = 30;   //画布的高度
    private $number = 4;    //验证码的字符个数
    private $font_file = 'STHUPO.TTF';  //验证码的字体文件
    private $font_size = 20;    //验证码的字体大小
    
    public function __set($p,$v)
    {
        if(property_exists($this, $p)){
           $this -> $p = $v; 
        }
    }
    public function __get($p)
    {
        if(property_exists($this, $p)){
            return $this -> $p;
        }
    }
    //开始绘制验证码
    public function makeImage()
    {
        //1. 创建画布,背景颜色应该是随机产生的,尽量背景颜色浅一点
        $image = imagecreatetruecolor($this->width, $this->height);
        //2. 分配颜色        
        $color = imagecolorallocate($image, mt_rand(100,255), mt_rand(100,255), mt_rand(100,255));
        imagefill($image, 0, 0, $color);
        
        //3. 开始绘制文字
        $code = $this->makeCode();
        for($i=0;$i<strlen($code);$i++){
            imagettftext($image, $this->font_size, mt_rand(-30,30), ($this->width/$this->number)*$i+5, 20, mt_rand(0,100), $this->font_file, $code[$i]);
            
        }        
        //绘制100个干扰像素点
        for($i=0;$i<100;$i++){
            imagesetpixel($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,100));
        }
        
        //练习:绘制10条干扰线条
        for($i=0;$i<10;$i++){
            $color = imagecolorallocate($image, mt_rand(100,150), mt_rand(100,150), mt_rand(100,150));
            imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $color);
        }
        
        //3. 输出到浏览器
        header("Content-Type:image/png");
        imagepng($image);
        //4. 销毁图像资源
        imagedestroy($image);
    }
    //生成随机的字符
    public function makeCode()
    {
        //大写字母,range()用来生成一个数组,包含从指定的开始字符到结束字符范围内的元素的数组
        $upper = range('A','Z');
        //小写字母
        $lower = range('a','z');
        //数字,避免0、1、2
        $number = range(3,9);
        //把3个数组合并成一个数组
        $code = array_merge($lower,$upper,$number);
        //打乱数组顺序
        shuffle($code);
        //根据属性中指定的字符个数,创建字符
        $str = '';
        for($i=0;$i<$this->number;$i++){
            $str .= $code[$i];
        }
        //echo '<pre>';
        //var_dump($str);
        return $str;
    }   
    
}

$captcha = new Captcha();
$captcha -> makeImage();

 

转载于:https://www.cnblogs.com/hzg8754/p/9756254.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值