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;
}
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章