1. 程式人生 > >PHP實現驗證碼封裝

PHP實現驗證碼封裝

<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/10/28
 * Time: 20:14
 */

/**
 *
 */
class Captcha
{
    /**
     * @param int $width =80
     * @param int $height =80
     * @param int $length =4
     * @param  type 1 - 數字 2-字母 3 - 數字 +字母 4 -漢字 5 - 數字運算
     */
    protected $image;
    protected $width;
    protected $height;
    protected $length;
    protected $fontFile;


    /**
     * @param int $width
     * @param int $height
     * @param int $length
     * @param $type 1 為數字 2 為字母 3 數字+ 字母 4  為漢子 5 為加法運算
     * @param $pixel 0 干擾點 1 干擾線 2 圓弧 3全部
     * @arr array 若 $jam=3,$arr = [干擾點,干擾線,圓弧];否則,$jam =2,$arr=
     */
    public function captcha($width = 80, $height = 80, $length = 4, $type = 0, $jam = [], $arr = [])
    {
//      初始化引數
        $this->width = $width;
        $this->length = $length;
        $this->height = $height;
        //建立畫布
        $this->image = imagecreatetruecolor($this->width, $this->height);
        $this->fontFile = 'fontawesome-webfont.ttf';

        $this->createCaptcha($jam,$arr);
    }

    public function createCaptcha($jam,$arr)
    {
        //建立顏色
        for ($i = 0; $i < $this->length; $i++) {

            $size = mt_rand(20, 28);
            $angle = mt_rand(-20, 60);
            $x = 20 + ceil($this->width / $this->length) * $i;
            $y = mt_rand($this->height / 3, $this->height - 20);

            $text = mb_substr(self::captchaType(), $i, 1, 'utf-8');
            imagettftext($this->image, $size, $angle, $x, $y, self::getRandColor($this->image), $this->fontFile, $text);
        }
        $this->jam($jam,$arr);
        //輸出影象到瀏覽器
        header('content-type:image/png');
        imagepng($this->image);
        imagedestroy($this->image);
    }

    /**
     * @param $image
     * @return  字型顏色
     */
    public static function getRandColor($image)
    {
        return imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    }

    /**
     *
     * @param $length
     * @param  type 1 - 數字 2-字母 3 - 數字 +字母 4 -漢字 5 - 數字運算
     * return $str 驗證碼裡面的內容
     */
    public static function captchaType($type = 1, $length = 4)
    {
        $str = "";
        switch ($type) {
            case 1:
                $str = join('', array_rand(array_flip(range(2, 9)), $length));
                break;
            case 2:
                $str = join('', array_rand(array_flip(array_merge(range('a', 'z'), range('A', "Z"))), $length));
                break;
            case 3:
                $str = join('', array_rand(array_merge(range(2, 9), array_flip(array_merge(range('a', 'z'), range('A', "Z")))), $length));
                break;
            case 4:
                $char = "就 若 朝 美 首 腦 會 談 推 遲 至 明 年 年 初 金 正 恩 原 定 於 年 內 ";
                $arr = str_split($char);
                $arr = array_flip(explode(" ", $char));
                $str = join('', array_rand($arr, $length));
                break;
            case 5:
                $num1 = array_rand(range(1, 100), 1);
                $num2 = array_rand(range(1, 100), 1);
                $str = $num1 . "+" . $num2 . "=";
                break;
            default:
                die("非法操作 !");
        }
        return $str;
    }

    /**
     * 新增干擾元素
     */
    public function jam($type=[0], $arr = [100, 0, 0])
    {
        for($i = 0;$i < $type.length;$i++){
        switch ($type[$i]) {
            case 0 :
                $this->createJam('setpixel', $arr[$i]);
                break;
            case 1:
                $this->createJam('line', $arr[$i]);
                break;
            case 2 :
                $this->createJam('arc', $arr[$i]);
                break;
            case 3 :
                $this->createJam('setpixel', $arr[0]);
                $this->createJam('line', $arr[1]);
                $this->createJam('arc',$arr[2]);
                break;
        }
    }

   }

    public function createJam($str, $param)
    {

        for ($i = 0; $i < $param; $i++) {
            if ($str == 'arc') {
                imagearc($this->image, mt_rand(0, $this->width / 2), mt_rand(0, $this->height / 2), mt_rand(0, $this->width / 2), mt_rand(0, $this->height / 2), mt_rand(0, 360), mt_rand(0, 360), self::getRandColor($this->image));
            } else {
                image . $str($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), self::getRandColor($this->image));
            }
        }
    }

}