PHP实现随机数字、字母的验证码功能
可自定义生成验证码文字的大小、数量、干扰项等等,也可以自定义验证文字的字体。。。
废话不多说,直接上代码:
1、classgd.class.php
0){ if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){ $this->_fontfile=$config['fontfile']; }else{ returnfalse; } if(isset($config['size'])&&$config['size']>0){ $this->_size=(int)$config['size']; } if(isset($config['width'])&&$config['width']>0){ $this->_width=(int)$config['width']; } if(isset($config['height'])&&$config['height']>0){ $this->_height=(int)$config['height']; } if(isset($config['length'])&&$config['length']>0){ $this->_length=(int)$config['length']; } if(isset($config['snow'])&&$config['snow']>0){ $this->_snow=(int)$config['snow']; } if(isset($config['pixel'])&&$config['pixel']>0){ $this->_pixel=(int)$config['pixel']; } if(isset($config['line'])&&$config['line']>0){ $this->_line=(int)$config['line']; } $this->_image=imagecreatetruecolor($this->_width,$this->_height); return$this->_image; } else{ returnfalse; } } publicfunctiongetCaptcha(){ $white=imagecolorallocate($this->_image,255,255,255); imagefilledrectangle($this->_image,0,0,$this->_width,$this->_height,$white); $str=$this->_generateStr($this->_length); if(false===$str){ returnfalse; } $fontfile=$this->_fontfile; for($i=0;$i<$this->_length;$i++){ $size=$this->_size; $angle=mt_rand(-30,30); $x=ceil($this->_width/$this->_length)*$i+mt_rand(5,10); $y=ceil($this->_height/1.5); $color=$this->_getRandColor(); //针对中文字符截取 //$text=mb_substr($str,$i,1,'utf-8'); $text=$str{$i}; imagettftext($this->_image,$size,$angle,$x,$y,$color,$fontfile,$text); } if($this->_snow){ $this->_getSnow(); }else{ if($this->_pixel){ $this->_getPixel(); } if($this->_line){ $this->_getLine(); } } header('content-type:image/png'); imagepng($this->_image); imagedestroy($this->_image); returnstrtolower($str); } privatefunction_getSnow(){ for($i=1;$i<=$this->_snow;$i++){ imagestring($this->_image,mt_rand(1,5),mt_rand(0,$this->_width),mt_rand(0,$this->_height),'*',$this->_getRandColor()); } } privatefunction_getPixel(){ for($i=1;$i<=$this->_pixel;$i++){ imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); } } privatefunction_getLine(){ for($i=1;$i<=$this->_line;$i++){ imageline($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); } } privatefunction_generateStr($length=4){ if($length<1||$length>30){ returnfalse; } $chars=array( 'a','b','c','d','e','f','g','h','k','m','n','p','x','y','z', 'A','B','C','D','E','F','G','H','K','M','N','P','X','Y','Z', 1,2,3,4,5,6,7,8,9 ); $str=join('',array_rand(array_flip($chars),$length)); return$str; } privatefunction_getRandColor(){ returnimagecolorallocate($this->_image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); } } ?>
2、testCaptcha.php
'fonts/simfang.ttf',//引入字体文件 //'snow'=>50, 'pixel'=>100, 'line'=>10 ); $captcha=newCaptcha($config); $captcha->getCaptcha(); ?>
总结
以上所述是小编给大家介绍的PHP实现随机数字、字母的验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。