温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP碎码——自己写的验证码

发布时间:2020-07-07 13:08:27 阅读:463 作者:lc994811089 栏目:web开发
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

其实里面没必要封装函数,只是当时觉得视觉上好看而已,结构清晰点

<?php       

class captcha{
    //验证码-字符串
    private $codes;
    //图片长度
    private $img_length 150;
    //图片高度
    private $img_height 30;
    //字符列表,用以生成随机验证码
    private $charlist '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
    //随机码的个数
    private $code_num 4;
    //字体大小--初始化时算出的
    private $font_size ;
    //干扰线数目
    private $line_num 5;
    //干扰雪花数目
    private $sterisk_num 50;
    //验证码--图片
    private $img;
    //字体文件路径
    private $ttf './instance/font/Elephant.ttf';
    
    public function __construct(){   
        
        //字体大小通过图片宽高动态生成的,但感觉不太完美
        $this->font_size = ($this->img_height*2/5 > $this->img_height*4/5 ? $this->img_height*4/5 : $this->img_height*2/5);
    }
    
    public function run(){
        //创建图片资源
        $this->createImage();

        //往图片中添加雪花
        $this->addaSterisk();
        //往图片中添加字符
        $this->addfont();
        //往图片中添加线条
        $this->addLine();
        //将图片输出至浏览器
        $this->outputImg();
    }
    //返回验证码字符串
    public function getCode(){
        return $this->codes;
    }
    
    //创建图片资源
    private function createImage(){
        //创建图片资源
        $this->img = p_w_picpathcreatetruecolor($this->img_length,$this->img_height);  
        //创建颜色
        $color_bg p_w_picpathcolorallocate($this->img, mt_rand(210255), mt_rand(210255), mt_rand(210255));
        //设置图片背景色
        p_w_picpathfill($this->img, 00$color_bg);
    }
    
    //往图片中添加线条
    private function addLine(){
        //添加指定数量的线条
        for ($i 0$i $this->line_num; $i++) {
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_line p_w_picpathcolorallocate($this->img, mt_rand(50200), mt_rand(50200), mt_rand(50200));
            
            //添加线条,位置随机--参数(图片资源,起点-x,起点-y,终点-x,终点-y,颜色)
            //不可调整
            //p_w_picpathline($this->img, mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), mt_rand(0, $this->img_length), mt_rand(0, $this->img_height), $color_line);
            //可以调整线条的粗细
            $src_x mt_rand(0$this->img_length);
            $src_y mt_rand(0$this->img_height);
            $dest_x mt_rand(0$this->img_length);
            $dest_y mt_rand(0$this->img_height);
            for ($j 0$j 1$j++) {
                p_w_picpathline($this->img, $src_x+$j$src_y+$j$dest_x+$j,$dest_y+$j$color_line);
            }
        }
    }
    //往图片中添加雪花
    private function addaSterisk(){
        //添加指定数量的雪花
        for ($i 0$i $this->sterisk_num; $i++) {
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_Ster p_w_picpathcolorallocate($this->img, mt_rand(220255), mt_rand(220255), mt_rand(220255));
            //添加雪花,位置随机--参数(图片资源,倾斜角度,左下角-x,左下角-y,颜色,字符串)
            p_w_picpathstring($this->img,mt_rand(0,360),mt_rand(0,$this->img_length),mt_rand(0,$this->img_height),'*',$color_Ster);
        }
    }
    
    private function addfont(){
        for ($i 0$i $this->code_num; $i++) {
            //随机从字符列表中取一个字符
            $code substr(str_shuffle($this->charlist),-1);
            //记录到验证码字符串中
            $this->codes .= $code;
            //创建随机颜色--参数(图片资源,R,B,G)
            $color_font p_w_picpathcolorallocate($this->img, mt_rand(10180), mt_rand(10180), mt_rand(10180));
            //添加雪花,位置随机--参数(图片资源,字体大小,倾斜角度,左下角-x,左下角-y,字体颜色,字体,字符串)
            // 左下角-y,字体的基准高度是估计的,由于字体大小使用磅,不同字符的长宽像素相差甚大
            p_w_picpathttftext($this->img, $this->font_size, mt_rand(-3030), ($this->img_length/$this->code_num)*$i+mt_rand(1,$this->font_size*0.2), $this->img_height*0.7+mt_rand(-$this->img_height*0.2$this->img_height*0.2), $color_font$this->ttf, $code);
        }
    }
    
    //输出图片至浏览器
    private function  outputImg(){
        //通知浏览器是png格式
        header('Content-type:p_w_picpath/png');
        //以png格式输出
        p_w_picpathpng($this->img);
        //销毁内存中的图片资源
        p_w_picpathdestroy($this->img);
    }
    
    public function __set($key,$value){
        
    }
    
    public function __get($value){
        
    }
    
    
}

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×