Cml\Vendor\VerifyCode::CnVerify PHP Метод

CnVerify() публичный статический Метод

中文验证码
public static CnVerify ( integer $length = 4, string $type = 'png', integer $width = 180, integer $height = 50, string $font = 'tahoma.ttf', string $verifyName = 'verifyCode' ) : void
$length integer
$type string
$width integer
$height integer
$font string
$verifyName string
Результат void
    public static function CnVerify($length = 4, $type = 'png', $width = 180, $height = 50, $font = 'tahoma.ttf', $verifyName = 'verifyCode')
    {
        $code = StringProcess::randString($length, 4);
        $width = $length * 45 > $width ? $length * 45 : $width;
        $authKey = md5(mt_rand() . microtime());
        Cookie::set($verifyName, $authKey);
        Model::getInstance()->cache()->set($authKey, md5($code), 1800);
        $im = imagecreatetruecolor($width, $height);
        $borderColor = imagecolorallocate($im, 100, 100, 100);
        //边框色
        $bkcolor = imagecolorallocate($im, 250, 250, 250);
        imagefill($im, 0, 0, $bkcolor);
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        // 干扰
        for ($i = 0; $i < 15; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
        }
        for ($i = 0; $i < 255; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
        }
        if (!is_file($font)) {
            $font = CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font;
        }
        for ($i = 0; $i < $length; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            $codex = StringProcess::substrCn($code, $i, 1);
            imagettftext($im, mt_rand(16, 20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $font, $codex);
        }
        self::output($im, $type);
    }