《2022年php图形验证码 .pdf》由会员分享,可在线阅读,更多相关《2022年php图形验证码 .pdf(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、php图形验证码类 复制代码字体文件见附件!SetImage(1,4,65,20,80,1);class vCodeImage /*FILENAME:verify_image.php *COPYRIGHT:NONE!(但请保留此信息)*AUTHOR:vsFree.Com *DATE:2007-08-08 */var$mode;/1:数字模式,2:字母模式,3:数字字母模式,其他:数字字母优化模式 var$v_num;/验证码个数名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 8 页 -var$img_w;/验证码图像宽度 var$img_h;/验证码图像高度 var$int_pi
2、xel_num;/干扰像素个数 var$int_line_num;/干扰线条数 var$font_dir;/字体文件相对路径 var$border;/图像边框 var$borderColor;/图像边框颜色 function SetImage($made,$v_num,$img_w,$img_h,$int_pixel_num,$int_line_num,$font_dir=./font,$border=true,$borderColor=255,200,85)if(!isset($_SESSIONvCode)session_register(vCode);$_SESSIONvCode=;$th
3、is-mode=$made;$this-v_num=$v_num;$this-img_w=$img_w;$this-img_h=$img_h;$this-int_pixel_num=$int_pixel_num;$this-int_line_num=$int_line_num;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 8 页 -$this-font_dir=$font_dir;$this-border=$border;$this-borderColor=$borderColor;$this-GenerateImage();function GetChar($mode)if(
4、$mode=1)$ychar=0,1,2,3,4,5,6,7,8,9;else if($mode=2)$ychar=A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;else if($mode=3)$ychar=0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z;else$ychar=3,4,5,6,7,8,9,A,B,C,D,H,K,P,R,S,T,W,X,Y;return$ychar;名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共
5、8 页 -function RandColor($rs,$re,$gs,$ge,$bs,$be)$r=mt_rand($rs,$re);$g=mt_rand($gs,$ge);$b=mt_rand($bs,$be);return array($r,$g,$b);function GenerateImage()$im=imagecreate($this-img_w,$this-img_h);$black=imagecolorallocate($im,0,0,0);$white=imagecolorallocate($im,255,255,255);$bgcolor=imagecoloralloc
6、ate($im,250,250,250);imagefill($im,0,0,$bgcolor);$fonts=ScanDir($this-font_dir);$fmax=count($fonts)-2;$ychar=$this-GetChar($this-mode);$list=explode(,$ychar);名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 8 页 -$x=mt_rand(2,$this-img_w/($this-v_num+2);$cmax=count($list)-1;$v_code=;for($i=0;$iv_num;$i+)/验证码$randnum=m
7、t_rand(0,$cmax);$this_char=$list$randnum;$v_code.=$this_char;$size=mt_rand(intval($this-img_w/5),intval($this-img_w/4);$angle=mt_rand(-20,20);$y=mt_rand($size+2),($this-img_h-2);if($this-border)$y=mt_rand($size+3),($this-img_h-3);$rand_color=$this-RandColor(0,200,0,100,0,250);$randcolor=imagecoloral
8、locate($im,$rand_color0,$rand_color1,$rand_color2);$fontrand=mt_rand(2,$fmax);$font=$this-font_dir/.$fonts$fontrand;imagettftext($im,$size,$angle,$x,$y,$randcolor,$font,$this_char);名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 8 页 -$x=$x+intval($this-img_w/($this-v_num+1);for($i=0;$iint_pixel_num;$i+)/干扰像素$rand_co
9、lor=$this-RandColor(50,250,0,250,50,250);$rand_color_pixel=imagecolorallocate($im,$rand_color0,$rand_color1,$rand_color2);imagesetpixel($im,mt_rand()%$this-img_w,mt_rand()%$this-img_h,$rand_color_pixel);for($i=0;$iint_line_num;$i+)/干扰线$rand_color=$this-RandColor(0,250,0,250,0,250);$rand_color_line=i
10、magecolorallocate($im,$rand_color0,$rand_color1,$rand_color2);imageline($im,mt_rand(0,intval($this-img_w/3),mt_rand(0,$this-img_h),mt_rand(intval($this-img_w-($this-img_w/3),$this-img_w),mt_rand(0,$this-img_h),$rand_color_line);if($this-border)/画出边框 if(preg_match(/d1,3,d1,3,d1,3$/,$this-borderColor)
11、名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 8 页 -$borderColor=explode(,$this-borderColor);$border_color_line=imagecolorallocate($im,$borderColor0,$borderColor1,$borderColor2);imageline($im,0,0,$this-img_w,0,$border_color_line);/上横 imageline($im,0,0,0,$this-img_h,$border_color_line);/左竖 imageline($im,0,$this-img_
12、h-1,$this-img_w,$this-img_h-1,$border_color_line);/下横 imageline($im,$this-img_w-1,0,$this-img_w-1,$this-img_h,$border_color_line);/右竖 imageantialias($im,true);/抗锯齿$time=time();$_SESSIONvCode=$v_code.|.$time;/把验证码和生成时间负值给$_SESSIONvCode /生成图像给浏览器 if(function_exists(imagegif)header(Content-type:image/g
13、if);imagegif($im);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 8 页 -elseif(function_exists(imagepng)header(Content-type:image/png);imagepng($im);elseif(function_exists(imagejpeg)header(Content-type:image/jpeg);imagejpeg($im,80);elseif(function_exists(imagewbmp)header(Content-type:image/vnd.wap.wbmp);imagewbmp($im);else die(No Image Support On This Server!);imagedestroy($im);名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 8 页 -