php之验证码

<?php
header("Content-type: p_w_picpath/png");
gd_info();
$num=5;//生成验证码的字母个数
$type=2;//验证码的类型,0表示只有数字,1表示数字和小写字母,2表示数字和大写字母
$str=getCode($num,$type);
$width=$num*20;
$hight=30;
$im=p_w_picpathcreatetruecolor($width,$hight);

$color=p_w_picpathcolorallocate($im,100,100,100);
$bg = p_w_picpathcolorallocate($im,255,2,255);
p_w_picpathfill($im,0,0,$bg);

###添加干扰点
for($i=0;$i<200;$i++){
        p_w_picpathsetpixel($im,rand(0,$width),rand(0,$hight),$color);
}
 
###添加干扰线
for($i=0;$i<5;$i++){
        p_w_picpathline($im,rand(0,$width),rand(0,$hight),rand(0,$width),rand(0,$hight),$color);
}

####在画布上画字符
for($i=0;$i<$num;$i++){
        p_w_picpathchar($im,1000,5+(18*$i),8,$str[$i],$color);//图像源,字体大小,倾斜角度(0为不倾斜),起始x坐标,起始y坐标,颜色,字体文件,内容
}

//ob_clean();//清除缓冲区内

p_w_picpathpng($im);
p_w_picpathdestroy($im);

function getCode($num,$type)
{
        $content="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $t=array(9,35,strlen($content)-1);
        $c="";
        for($i=0;$i<$num;$i++)
        {
                $c.=$content[rand(0,$t[$type])];
        }
        return $c;
}
?>
相关文章
相关标签/搜索