1 <?php 2 header("content-type:image/jpeg"); 3 4 session_start();//开启session 5 6 //宽高 字体大小 7 $width=120; 8 $height=40; 9 $fontsize=20; 10 11 //给画布宽高 12 $img=imagecreatetruecolor($width,$height); 13 14 //画布背景色 15 $bgcolor=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255)); 16 17 //画布边框背景 18 $bordercolor=imagecolorallocate($img,rand(0,155),rand(0,155),rand(0,155)); 19 20 //内容字体颜色 21 $fontcolor=imagecolorallocate($img,rand(30,100),rand(30,100),rand(30,100)); 22 23 //填充背景色 24 imagefill($img,0,0,$bgcolor); 25 26 //填充边框 27 imagerectangle($img,0,0,$width-1,$height-1,$bordercolor); 28 29 //干扰点 30 for($a=0;$a<100;$a++){ 31 imagesetpixel($img,rand(1,$width-2),rand(1,$height-2),$bordercolor); 32 } 33 34 //创建数组 35 $shuzi=range(0,9); 36 37 //随机数 38 $suiji=$shuzi[rand(0,count($shuzi)-1)].$shuzi[rand(0,count($shuzi)-1)] 39 .$shuzi[rand(0,count($shuzi)-1)].$shuzi[rand(0,count($shuzi)-1)]; 40 41 //输出内容 42 imagettftext($img,$fontsize,rand(-5,5),rand(15,25),rand(28,33),$fontcolor,'ziti.ttf',$suiji);$_SESSION['yzm_b']=$suiji; 43 44 //获取‘验证码输入框的内容’ 45 46 47 48 //输出画布 49 imagejpeg($img); 50 ?>