PHP 生成图像验证码,个性化参数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QQ80583600/article/details/70835163

<?php
// 产生一个随机字符串
function buildRandomString($type = 1, $length = 4) {
    if ($type == 1) {
        $chars = join("", range(0, 9));
    } elseif ($type == 2) {
        // array_merge 把两个数组合并为一个数组:
        $chars = join("", array_merge(range("A", "Z"), range("A", "Z")));
    } elseif ($type == 3) {
        $chars = join("", array_merge(range("A", "Z"), range(0, 9)));
    } elseif ($type == 4) {
        $chars = join("", array_merge(range("a", "z"), range("A", "Z"), range(0, 9)));
    }

    if ($length > strlen($chars)) {
        exit("字符串长度不够");
    }

    // 随机地打乱字符串中的所有字符:
    $chars = str_shuffle($chars);

    // 截取字符串
    return substr($chars, 0, $length);
}

// 使用GD库做验证码
function verifyImage($type = 1, $length = 4, $pixel = 1, $line = 0) {
    // session_start();

    $width  = 100; // 画布宽
    $height = 40; // 画布高
    $image  = imagecreatetruecolor($width, $height); // 创建验证码画布

    $black        = imagecolorallocate($image, 0x00, 0x00, 0x00);
    $green        = imagecolorallocate($image, 0x00, 0xFF, 0x00);
    $blue         = imagecolorallocate($image, 0x00, 0x00, 0xFF);
    $white        = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
    $random_color = imagecolorallocate($image, mt_rand(0, 200), mt_rand(0, 200), mt_rand(0, 200)); // 随机颜色
    $fontfiles    = array("FZSTK.TTF", "msyh.ttc", "msyhbd.ttc", "msyhl.ttc", "simsun.ttc", "SIMYOU.TTF", "STXINGKA.TTF"); // 随机字体

    // 矩形区域着色
    imagefill($image, 0, 0, $white);
    // imagefilledrectangle($image, 0, 0, $width, $height, $white);

    // 产生一个随机字符串
    // $type   = 1; // 验证码类型
    // $length = 4; // 验证码长度
    $chars  = buildRandomString($type, $length);

    // 保存字符串,以待验证
    $sess_name            = "verify";
    $_SESSION[$sess_name] = $chars;

    // 将字符串写到图片中
    for ($i = 0; $i < $length; $i ++) {
        $size     = mt_rand(20, 25); // 字符字号大小
        $angle    = mt_rand(- 15, 15); // 旋转角度
        $x        = 5 + $i * $size; // x y 两参数为文字的坐标值 (原点为左上角)
        $y        = mt_rand(28, 38);
        $color    = $random_color;
        $fontfile = "../fonts/" . $fontfiles[mt_rand(0, count($fontfiles) - 1)];
        $text     = substr($chars, $i, 1);
        imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text); // 写 TTF 文字到图中。
    }

    // 加入噪点干扰(可选)
    // $pixel = true;
    if ($pixel) {
        for($i = 0; $i < 30; $i ++) {
          imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $black); 
          imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $green);
          imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), $blue);
        }
    }

    // 加入直线干扰(可选)
    // $line = false;
    if ($line) {
        for($i = 0; $i < 1; $i ++) {
          imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $black);
          imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $green);
          imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $blue);
        }
    }

    // 输出验证码
    ob_clean();
    header("content-type: image/png");
    imagepng($image);
    imagedestroy($image);
}

verifyImage(1, 4, 1, 0);

猜你喜欢

转载自blog.csdn.net/QQ80583600/article/details/70835163
今日推荐