«

php生成验证码图像

MitSeek 发布于 阅读:3


    function captcha_show($code)
    {
    // 创建图片资源,随机生产背景颜色
    $x = 250;
    $y = 62;
    $im = imagecreate($x, $y);
    imagecolorallocate($im, rand(50,200), rand(0,155), rand(0,155));

    // 设置字体颜色和样式
    $fontColor = imagecolorallocate($im, 255, 255, 255); // 修正变量名
    $fontStyle = COMMON_PATH . 'file/capthcha.ttf'; // 注意字体文件路径,可能需要修正拼写

    // 生成指定长度的验证码
    for ($i = 0, $len = strlen($code); $i < $len; ++$i) {
        imagettftext(
            $im,
            30, 
            rand(0,20) - rand(0,25), 
            32 + $i * 40, 
            rand(30,50), 
            $fontColor,   // 使用修正的变量名
            $fontStyle,   // 使用修正的变量名
            $code[$i]
        );
    }

    // 添加8条干扰线
    for ($i = 0; $i < 8; ++$i) {
        $lineColor = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
        imageline($im, rand(0, $x), 0, rand(0, $x), $y, $lineColor);
    }

    // 添加250个噪点
    for ($i = 0; $i < 250; ++$i) { // 修正循环条件
        imagesetpixel($im, rand(0, $x), rand(0, $y), $fontColor); // 使用修正的变量名
    }

    // 向浏览器输出验证码图片
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
}

代码逐行解释

function captcha_show($code)
{
    // 创建图片资源,随机生成背景颜色
    $im = imagecreate($x = 250, $y = 62);

imagecreate() - 创建新的空白图像,参数是宽度和高度

创建250x62像素的图像,同时定义变量$x和$y

    imagecolorallocate($im, rand(50,200), rand(0,155), rand(0,155));

imagecolorallocate() - 为图像分配颜色

rand(50,200), rand(0,155), rand(0,155) - 随机生成RGB颜色值

第一次调用imagecolorallocate()会设置图像背景色


    // 设置字体颜色和样式
    $fontcolor = imagecolorallocate($im, 255, 255, 255);
    $fontStyle = COMMON_PATH.'file/capthcha.ttf';

分配白色(255,255,255)作为字体颜色

设置TrueType字体文件路径

 // 生成指定长度的验证码
    for($i = 0, $len = strlen($code); $i < $len; ++$i)
    {
        imagettftext($im,
        30,  // 字符尺寸
        rand(0,20) - rand(0,25),  // 随机设置字符倾斜角度
        32 + $i * 40, rand(30,50), // 随机设置字符坐标
        $fontColor,   // 字符颜色
        $fontstyle,  // 字符样式
        $code[$i]     // 字符内容
        );
    }

strlen($code) - 获取验证码字符串长度

imagettftext() - 用TrueType字体向图像写入文本

参数:图像资源、字体大小、角度、x坐标、y坐标、颜色、字体文件、文本内容

每个字符随机倾斜,x坐标递增,y坐标随机


    // 添加8条干扰线
    for($i = 0; $i < 8; ++$i)
    {
        // 随机生成干扰线颜色
        $lineColor = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
        // 随机生成干扰线
        imageline($im, rand(0,$x), 0, rand(0,$x), $y, $lineColor);
    }

imageline() - 画一条线段

在随机位置画8条不同颜色的干扰线

   // 添加250个噪点
    for($i = 0; $i < 250; ++$i)
    {
        // 随机生成噪点位置
        imagesetpixel($im, rand(0,$x), rand(0,$y), $fontColor);
    }

imagesetpixel() - 画一个单一像素点

在随机位置画250个白色噪点


    // 向浏览器输出验证码图片
    header('Content-type: image/png');  // 设置发送的信息头内容
    imagepng($im);   // 输出图片
    imagedestroy($im);  // 释放图片所占内存
}

header() - 发送HTTP头,告诉浏览器这是PNG图片

imagepng() - 以PNG格式输出图像到浏览器或文件

imagedestroy() - 销毁图像资源,释放内存

涉及的主要函数总结

imagecreate() - 创建新图像

imagecolorallocate() - 分配颜色

imagettftext() - 用TrueType字体写文字

imageline() - 画直线

imagesetpixel() - 画像素点

imagepng() - 输出PNG图像

imagedestroy() - 销毁图像资源

rand() - 生成随机数

strlen() - 获取字符串长度

header() - 发送HTTP头

这个函数通过随机背景色、字符倾斜、干扰线和噪点来增强验证码的安全性,防止机器识别。

php