Cml\Vendor\VerifyCode::calocVerify PHP Method

calocVerify() public static method

生成数字计算题验证码
public static calocVerify ( string $type = 'png', integer $width = 170, integer $height = 45, string $font = 'tahoma.ttf', string $verifyName = 'verifyCode' ) : void
$type string
$width integer
$height integer
$font string
$verifyName string
return void
    public static function calocVerify($type = 'png', $width = 170, $height = 45, $font = 'tahoma.ttf', $verifyName = 'verifyCode')
    {
        $la = rand(0, 9);
        $ba = rand(0, 9);
        $randnum = rand(1, 3);
        if ($randnum == 3) {
            if ($la < $ba) {
                $tmp = $la;
                $la = $ba;
                $ba = $tmp;
            }
        }
        $randarr = [1 => $la + $ba, 2 => $la * $ba, 3 => $la - $ba];
        $randstr = $randarr[$randnum];
        $randResult = [1 => $la . '+' . $ba . '=?', 2 => $la . '*' . $ba . '=?', 3 => $la . '-' . $ba . '=?'];
        $randval = $randResult[$randnum];
        $authKey = md5(mt_rand() . microtime());
        Cookie::set($verifyName, $authKey);
        Model::getInstance()->cache()->set($authKey, $randstr, 1800);
        //$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = imagecreatetruecolor($width, $height);
        } else {
            $im = imagecreate($width, $height);
        }
        $r = array(225, 255, 255, 223);
        $g = array(225, 236, 237, 255);
        $b = array(225, 236, 166, 125);
        $key = mt_rand(0, 3);
        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);
        //背景色(随机)
        $borderColor = imagecolorallocate($im, 100, 100, 100);
        //边框色
        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        // 干扰
        for ($i = 0; $i < 10; $i++) {
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
        }
        for ($i = 0; $i < 25; $i++) {
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
        }
        for ($i = 0; $i < 5; $i++) {
            //  imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
            $x = $i === 0 ? 20 : $i * 50;
            $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
            if ($i == 1 || $i == 3 || $i == 4) {
                $fontSize = $randnum == 3 ? 50 : 28;
                if ($i == 1) {
                    imagettftext($im, $fontSize, 0, $x, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $randval[$i]);
                } else {
                    $decNum = $i == 3 ? 30 : 55;
                    imagettftext($im, 25, 0, $x - $decNum, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $randval[$i]);
                }
            } else {
                imagettftext($im, 28, mt_rand(0, 60), $x, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $randval[$i]);
            }
        }
        self::output($im, $type);
    }