php生成扭曲字体的图片验证码函数

本函数需要用到字体文件 “arial.ttf” 将该文件放在与该函数所在文件的同一级目录下

/**
 *  CaptchaImage() - 创建扭曲字符的验证码图片
 *  $session_name string 验证码图片创建时所需生成Session的变量名
 *  $width int 验证图片的宽度,默认120,注:图片高度与宽度比例相对固定
 *  $noise int 干扰素的点数,默认0
 *  $disturb int 干扰字符个数,默认0
 *  $curve bool 是否增加干扰曲线,默认ture
 * */
function CaptchaImage($session_name = '',  $width = 120, $noise = 0, $disturb = 0, $curve = true){
    $im_x = $width;
    $im_y = ceil(0.25 * $width);
    $im = imagecreatetruecolor($im_x, $im_y);
    $text_c = ImageColorAllocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
    $gray_rand = mt_rand(160, 220);
    $buttum_c = ImageColorAllocate($im, $gray_rand, $gray_rand, $gray_rand);
    imagefill($im, 0, 0, $buttum_c);
    
    session_start();
    $text = '';
    $characters = 'ABCEFGHJKLMNPQRSTUVWXYZ';
    for($i = 0, $len = strlen($characters); $i < 4; $i++){
        $text .= $characters{rand(0, $len - 1)};
        if(isset($session_name{0}) && session_start()){
            $_SESSION[$session_name] = $text;
            $_SESSION['CaptchaSessionTime'] = time();
        }
    }
    
    $font = 'arial.ttf';
    $size = floor(0.2 * $width);
    
    $ttfbox = imagettfbbox($size, 0, $font, $text);
    $text_width = abs($ttfbox[2] - $ttfbox[0]);
    $side = floor(($im_x - $text_width) / 2);
    
    $array = array(-1, 1);
    for ($i = 0; $i < strlen($text); $i++){
        $p = array_rand($array);
        $an = $array[$p] * mt_rand(5, 10);      // 字符倾斜角度
        imagettftext($im, $size, $an, $side + $i * ($size - 1), $im_y - 3, $text_c, $font, $text{$i});
    }

    $distortion_im = imagecreatetruecolor ($im_x, $im_y);
    imagefill($distortion_im, 0, 0, $buttum_c);
    $distortion = floor(0.04 * $width);         // 扭曲程度
    for ($i = 0; $i < $im_x; $i++){
        for ($j = 0; $j < $im_y; $j++){
            $rgb = imagecolorat($im, $i , $j);
            if( (int)($i + sin($j / $im_y * 2 * M_PI) * $distortion) <= $im_x && (int)($i + sin($j / $im_y * 2 * M_PI) * $distortion) >= 0 ){
                imagesetpixel($distortion_im, (int)($i + sin($j / $im_y * 2 * M_PI - M_PI * 0.1) * $distortion) , $j, $rgb);
            }
        }
    }
    
    if($disturb > 0){
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        for($i = 0; $i < $disturb; $i++){
            imagestring($distortion_im, 3, mt_rand(-10, $im_x), mt_rand(-10, $im_y), $chars[mt_rand(0, 35)], $text_c);
        }
    }

    //加入干扰象素;
    if($noise > 0){
        for($i = 0; $i < $noise; $i++){
            $randcolor = ImageColorallocate($distortion_im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
            imagesetpixel($distortion_im, mt_rand()%$im_x , mt_rand()%$im_y , $randcolor);
        }
    }
    
    // 加干扰曲线
    if($curve){
        $rand = mt_rand(5, 30);
        $rand1 = mt_rand(15, 25);
        $rand2 = mt_rand(5, 10);
        for ($yy = $rand; $yy <= $rand + 2; $yy++){
            for ($px = -60; $px <= 60; $px++){
                $x = $px / $rand1;
                $y = ($x != 0) ? sin($x) : $y;
                $py = $y * $rand2;
                imagesetpixel($distortion_im, $px + 60, $py + $yy, $text_c);
            }
        }
    }
    
    //设置文件头;
    Header("Content-type: image/JPEG");

    //以PNG格式将图像输出到浏览器或文件;
    ImagePNG($distortion_im);

    //销毁一图像,释放与image关联的内存;
    ImageDestroy($distortion_im);
    ImageDestroy($im);
}

CaptchaImage('code', 100);//生成一张图片 并将随机数字存放到key为code的session中

附件下载:字体文件arial.ttf

留下评论

鄂ICP备13000209号-1

鄂公网安备 42050602000277号