create_code.phpphp
<?phpsession
session_start();ide
//生成驗證碼圖片code
header("Content-type: p_w_picpath/png");圖片
// 全數字string
$str = "1,2,3,4,5,6,7,8,9,a,b,c,d,f,g"; //要顯示的字符,可本身進行增刪it
$list = explode(",", $str);io
$cmax = count($list) - 1;驗證碼
$verifyCode = '';class
for ( $i=0; $i < 5; $i++ ){
$randnum = mt_rand(0, $cmax);
$verifyCode .= $list[$randnum]; //取出字符,組合成爲咱們要的驗證碼字符
}
$_SESSION['code'] = $verifyCode; //將字符放入SESSION中
$im = p_w_picpathcreate(58,28); //生成圖片
$black = p_w_picpathcolorallocate($im, 0,0,0); //此條及如下三條爲設置的顏色
$white = p_w_picpathcolorallocate($im, 255,255,255);
$gray = p_w_picpathcolorallocate($im, 200,200,200);
$red = p_w_picpathcolorallocate($im, 255, 0, 0);
p_w_picpathfill($im,0,0,$white); //給圖片填充顏色
//將驗證碼繪入圖片
p_w_picpathstring($im, 5, 10, 8, $verifyCode, $black); //將驗證碼寫入到圖片中
for($i=0;$i<50;$i++) //加入干擾象素
{
p_w_picpathsetpixel($im, rand()p , rand()0 , $black); //加入點狀干擾素
p_w_picpathsetpixel($im, rand()p , rand()0 , $red);
p_w_picpathsetpixel($im, rand()p , rand()0 , $gray);
//p_w_picpatharc($im, rand()p, rand()p, 20, 20, 75, 170, $black); //加入弧線狀干擾素
//p_w_picpathline($im, rand()p, rand()p, rand()p, rand()p, $red); //加入線條狀干擾素
}
p_w_picpathpng($im);
p_w_picpathdestroy($im);
?>