PHP实现随机数字、字母的验证码功能


Posted in PHP onAugust 01, 2018

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

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

1、classgd.class.php

<?php
Class Captcha{
    private $_fontfile='';
    private $_size=36;
    private $_width=200;
    private $_height=100;
    private $_length=4;
    private $_image=null;
    private $_snow=0;
    private $_pixel=0;
    private $_line=0;
  public function __construct($config=array()){
    if(is_array($config)&&count($config)>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

<?php
require_once 'classgd.class.php';
$config=array(
'fontfile'=>'fonts/simfang.ttf',  //引入字体文件
//'snow'=>50,
'pixel'=>100,
'line'=>10
  );
$captcha=new Captcha($config);
$captcha->getCaptcha();
?>

总结

以上所述是小编给大家介绍的PHP实现随机数字、字母的验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
Apache设置虚拟WEB
Oct 09 PHP
一个简单的PHP入门源程序
Oct 09 PHP
PHP中动态HTML的输出技术
Oct 09 PHP
杏林同学录(二)
Oct 09 PHP
PHP入门
Oct 09 PHP
php购物车实现代码
Oct 10 PHP
PHP正则表达式之定界符和原子介绍
Oct 05 PHP
php从完整文件路径中分离文件目录和文件名的方法
Mar 13 PHP
php多线程实现方法及用法实例详解
Oct 26 PHP
PHP单例模式与工厂模式详解
Aug 29 PHP
thinkPHP框架中layer.js的封装与使用方法示例
Jan 18 PHP
laravel框架创建授权策略实例分析
Nov 22 PHP
PHP使用XMLWriter读写xml文件操作详解
Jul 31 #PHP
laravel + vue实现的数据统计绘图(今天、7天、30天数据)
Jul 31 #PHP
PHP常用日期加减计算方法实例小结
Jul 31 #PHP
ThinkPHP5.0多个文件上传后找不到临时文件的修改方法
Jul 30 #PHP
PHP笛卡尔积实现算法示例
Jul 30 #PHP
作为PHP程序员你要知道的另外一种日志
Jul 30 #PHP
详解Laravel5.6 Passport实现Api接口认证
Jul 27 #PHP
You might like
第二节 对象模型 [2]
2006/10/09 PHP
建立动态的WML站点(三)
2006/10/09 PHP
PHP return语句的另一个作用
2014/07/30 PHP
详解PHP数组赋值方法
2015/11/07 PHP
CI框架附属类用法分析
2018/12/26 PHP
PHP根据key删除数组中指定的元素
2019/02/28 PHP
javascript 传统事件模型构造的事件监听器实现代码
2010/05/31 Javascript
js的一些常用方法小结
2011/06/29 Javascript
javascript加号&quot;+&quot;的二义性说明
2013/03/04 Javascript
JavaScript实现获取dom中class的方法
2015/02/09 Javascript
javascript原型模式用法实例详解
2015/06/04 Javascript
jQuery-1.9.1源码分析系列(十)事件系统之事件体系结构
2015/11/19 Javascript
基于jQuery实现点击列表加载更多效果
2016/05/31 Javascript
AngularJS实现页面定时刷新
2017/03/14 Javascript
BootStrap中jQuery插件Carousel实现轮播广告效果
2017/03/27 jQuery
Vue框架下引入ActiveX控件的问题解决
2019/03/25 Javascript
使用Vue+Django+Ant Design做一个留言评论模块的示例代码
2020/06/01 Javascript
vue组件是如何解析及渲染的?
2021/01/13 Vue.js
Python实现的桶排序算法示例
2017/11/29 Python
Python 实现一行输入多个值的方法
2018/04/21 Python
python实现两张图片拼接为一张图片并保存
2019/07/16 Python
python图像处理模块Pillow的学习详解
2019/10/09 Python
python利用faker库批量生成测试数据
2020/10/15 Python
python3爬虫中多线程的优势总结
2020/11/24 Python
python 发送邮件的示例代码(Python2/3都可以直接使用)
2020/12/03 Python
 Alo Yoga官网:购买瑜伽服装
2018/06/17 全球购物
叙述DBMS对数据控制功能有哪些
2016/06/12 面试题
酷瑞网络科技面试题
2012/03/30 面试题
Windows和Linux动态库应用异同
2016/07/28 面试题
工商企业管理应届生求职信
2013/11/03 职场文书
妇科医生自荐信
2013/11/05 职场文书
竞选生活委员演讲稿
2014/04/28 职场文书
合伙经营协议书范本
2014/09/13 职场文书
2015年医院护理部工作总结
2015/04/23 职场文书
Golang 获取文件md5校验的方法以及效率对比
2021/05/08 Golang
MySQL数据库如何给表设置约束详解
2022/03/13 MySQL