1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| <?php
//必须至于顶部,多服务器端记录验证码信息,便于用户输入后做校验 function vocode($width=120,$height=40,$fontsize=30){ session_start(); //默认返回的是黑色的照片 $image = imagecreatetruecolor($width, $height); //设置各种颜色 $bgcolor = imagecolorallocate($image, 255, 255, 255); $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120)); $stringcolor=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)); //将白色铺满地图 imagefill($image, 0, 0, $bgcolor); //空字符串,每循环一次,追加到字符串后面 $captch_code=''; $data=str_split("abcdefghijkmnpqrstuvwxyz3456789QWERTYUIOPASDFGHJKLZXCVNBM"); $len_data=count($data); //验证码为随机四个数字 for ($i=0; $i < 4; $i++) { // 产生随机数字0-9 $fontcontent = $data[rand(0,$len_data-1)]; $captch_code.= $fontcontent; } $the_font="./front_style/ManyGifts.ttf"; imagettftext($image,$fontsize,rand(-5,5),rand(5,15),rand(30,35),$fontcolor,$the_font,$captch_code);
$_SESSION['authcode'] = $captch_code; #画点 for ($i = 0; $i < 50; $i++) { imagesetpixel($image,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255))); } // 画线 for ($i = 0; $i < rand(2,3); $i++) { imageline($image,rand(0,$width/2-1),rand(0,$height-1),rand($width/2+1,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255))); } header('content-type:image/png'); imagepng($image); //销毁 imagedestroy($image); } ?><?php // 返回图片 header('Content-type:image/png'); // 设置宽度 高度 $width=200; $height=50; // 产生一个图像 $image=imagecreatetruecolor($width,$height); // 设置不同的颜色 $Bgcolor=imagecolorallocate($image,255,255,255); $fontcolor=imagecolorallocate($image,0,0,0); // 填充图像 imagefill($image,0,0,$Bgcolor); #画点 for ($i = 0; $i < 50; $i++) { imagesetpixel($image,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255))); }
// 画线 for ($i = 0; $i < rand(2,3); $i++) { imageline($image,rand(0,$width/2-1),rand(0,$height-1),rand($width/2+1,$width-1),rand(0,$height-1),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255))); } // 设置字 $the_font='arial.ttf'; // 设置字的 大小 角度 起始位置 颜色 字体 内容 imagettftext($image,20,20,0,0,$fontcolor,$the_font,'AAA'); #生成图像 imagepng($image); // 销毁 imagedestroy($image);
|