博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php实现随机数字、字母的验证码
阅读量:7248 次
发布时间:2019-06-29

本文共 3941 字,大约阅读时间需要 13 分钟。

 php实现随机数字、字母的验证码

   可自定义生成验证码文字的大小、数量、干扰项等等,也可以自定义验证文字的字体。。。

  废话不多说,直接上代码:

1、classgd.class.php
0){ if(isset($config['fontfile'])&&is_file($config['fontfile'])&&is_readable($config['fontfile'])){ $this->_fontfile=$config['fontfile']; }else{ return false; } 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{ return false; } } public function getCaptcha(){ $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){ return false; } $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); return strtolower($str); } private function _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()); } } private function _getPixel(){ for($i=1;$i<=$this->_pixel;$i++){ imagesetpixel($this->_image,mt_rand(0,$this->_width),mt_rand(0,$this->_height),$this->_getRandColor()); } } private function _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()); } } private function _generateStr($length=4){ if($length<1 || $length>30){ return false; } $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; } private function _getRandColor(){ return imagecolorallocate($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=new Captcha($config);$captcha->getCaptcha();?>

 

转载于:https://www.cnblogs.com/mycognos/p/9296993.html

你可能感兴趣的文章
优酷在多模态内容理解上的研究及应用
查看>>
JavaScript学习笔记整理:对象篇
查看>>
GitHub的bug赏金计划升级:奖金提高到3万美元以上
查看>>
中国法院裁定:禁售部分型号苹果手机
查看>>
使用实体框架、Dapper和Chain的仓储模式实现策略
查看>>
HTTP/2推送之难,远超想象
查看>>
与Bob McWhirter的问答:WildFly Swarm更名为Thorntail项目
查看>>
新书问答:Agile Management
查看>>
在sublime中实现代码检测
查看>>
活在伟大的Scrum团队是什么感觉
查看>>
Swift 5进入发布倒计时
查看>>
一套代码称霸5大端口,移动金融应用还能这样开发?
查看>>
MIT开发Polaris,使网页载入加快34%
查看>>
微软对macOS和Linux开放量子开发工具集
查看>>
一份关于Angular的倡议清单
查看>>
没有估算,你仍然可以用这些决策策略
查看>>
通过调研开源基准测试集,解读大数据的应用现状和开源未来
查看>>
译文-调整G1收集器窍门
查看>>
时序数据库InfluxDB 2.0 alpha 发布:主推新的Flux查询语言,TICK栈将成为整体
查看>>
开源是项“全民工程”,揭秘开源团队的管理运作
查看>>