PHP生成4位数验证码 <?php //创建一个底图(默认黑色背景) $image = imagecreatetruecolor(100,30); //在这个底图上创建像素点 $bgcolor = imagecolorallocate($image,255,255,255);//#FFFFFF //将这个像素填充到底图上去 imagefill($image,0,0,$bgcolor); //添加4位数字的验证码 for($i=0;$i<4;$i++){ $fontsize = 6; $fontcolor = imagecolorallocate($image,rand(1,120),rand(1,120), rand(1,120)); $fontcontent = rand(0,9); $x = ($i*100/4)+rand(5,10); $y = rand(5,10); imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); } //添加100个干扰点 for ($i=0;$i<200;$i++){ $pointcolor = imagecolorallocate($image,rand(50,200),rand(50,200), rand(50,200)); imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor); } //添加3条干扰线 for($i=0;$i<3;$i++){ $linecolor = imagecolorallocate($image,rand(80,220),rand(80,220),rand(80,220)); imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor); } //告知浏览器将要输出什么内容 header('content-type:image/png'); //显示图片 imagepng($image); //销毁图片 imagedestroy();