PHP类封装之验证码类封装

[ PHP ] 2019-11-19 21:34:11 杨歆 724
<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 验证码类文件
 * +--------------------------------------------------------------
 */

namespace luo;


class Captcha
{

    private $width = 120;

    private $height = 30;

    private $image = null;

    private $length = 4;

    private $fontsize = 24;

    private $en = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

    private $zh = '工了以在有地一上不是中国同民为这我的要和产发人经主';

    private $isZh = false;

    private $fontttf = 'fonts/1.ttf';

    public $isPixel = true;

    public $isLine = true;

    private $keys = 'mvcphp.v1.1.0';

    /**
     * Captcha constructor.
     * @param array $config
     */
    public function __construct(Array $config = [])
    {

        foreach($config as $key=>$val){
            if(isset($this->$key)){
                $this->$key = $val;
            }
        }
        if(session_status() != PHP_SESSION_ACTIVE){
            session_start();
        }

    }

    public function entry(){

        $this->create();
        $this->wText();
        if($this->isPixel)$this->pixel();
        if($this->isLine)$this->line();
        $this->view();

    }

    /**
     * @param $arg
     * @return bool
     */
    public function check($arg){

        $captcha_code = isset($_SESSION['captcha_code']) ? $_SESSION['captcha_code'] : false;
        if($this->mmd5(strtolower($arg)) == $captcha_code){
            return true;
        }
        return false;

    }

    /**
     * @param $string
     * @return string
     */
    private function mmd5($string){

        $string = md5($string);
        $string = mb_substr(
            $string, 
            (mb_strlen($string) / 2), 
            (mb_strlen($string) - 1)) . mb_substr($string, 0, (mb_strlen($string) / 2 - 1)
        );
        $string = md5($string);
        $string = md5($string . $this->keys);
        return $string;

    }

    private function create(){

        $image = imagecreatetruecolor($this->width, $this->height);
        $bg = imagecolorallocate($image, 255, 255, 255);
        imagefill($image, 0, 0, $bg);
        $this->image = $image;

    }

    private function wText(){
        $captcha_code = '';
        $len = mb_strlen($this->isZh ? $this->zh : $this->en);
        for($i=0;$i<$this->length;$i++){
            //设置字体颜色,随机颜色
            $fontcolor = imagecolorallocate($this->image, rand(0,120),rand(0,120), rand(0,120));      //0-120深颜色
            //$char = mb_substr($this->en, );
            $char = mb_substr(($this->isZh ? $this->zh : $this->en), mt_rand(0, $len - 1), 1, 'utf-8');
            //$char = $this->en{mt_rand(0, $len - 1)};
            //设置数字
            $fontcontent = $char;
            //10>.=连续定义变量
            $captcha_code .= $fontcontent;
            //设置坐标
            $x = ($i * $this->fontsize) + ($this->width - $this->length * $this->fontsize) / 2;
            //$y = rand(50,70);
            $y = ($this->height + $this->fontsize) / 2;

            imagettftext (
                $this->image,
                rand($this->fontsize - 5, $this->fontsize),
                rand(- $this->fontsize, $this->fontsize),
                $x,
                $y,
                $fontcolor,
                FRAME_PATH . $this->fontttf,
                $fontcontent
            );
        }
        $_SESSION['captcha_code'] = $this->mmd5(strtolower($captcha_code));
    }

    private function pixel(){

        $number = $this->fontsize * 10;
        // 增加干扰元素,设置雪花点
        for($i = 0; $i < $number; $i++){
            // 设置点的颜色,50-200颜色比数字浅,不干扰阅读
            $pointcolor = imagecolorallocate($this->image,rand(50,200), rand(50,200), rand(50,200));
            // imagesetpixel — 画一个单一像素
            imagesetpixel($this->image, rand(1,$this->width), rand(1,$this->height), $pointcolor);
        }

    }

    private function line(){
        $number = $this->length * 2;
        // 增加干扰元素,设置横线
        for($i = 0; $i < $number; $i++){
            //设置线的颜色
            $linecolor = imagecolorallocate($this->image,rand(80,220), rand(80,220),rand(80,220));
            //设置线,两点一线
            imageline(
                $this->image,
                rand(1, $this->width),
                rand(1, $this->height),
                rand(1, $this->width),
                rand(1, $this->height),
                $linecolor
            );
        }
    }

    private function view(){

        // 设置头部,image/png
        header('Content-Type: image/png');
        // imagepng() 建立png图形函数
        imagepng($this->image);
        // imagedestroy() 结束图形函数 销毁$image
        imagedestroy($this->image);
        exit;

    }

    /**
     * @param $name
     * @param $value
     */
    public function __set($name, $value){

        if(isset($this->$name)){
            $this->$name = $value;
        }

    }

}

支付宝支付
打赏
微信支付

联系我吧

有些人是会淡出你的生活的,有些人刻意不再联系,是因为你知道ta不需要你,或者从一开始就不是同一个初衷在一起度过,不主动联系很久也不见一次面,所有的感情也罢,都会败给时间和距离。因为时间是最无情,给你后悔的时刻却不会给你改过重来的机会。

位置

成都市 四川省 中国

联系电话

028-6406-0000

邮箱

y139w@163.com

企鹅

27572048

网址

https://www.weania.com