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 相关文章推荐
PHP 中的一些经验积累
Oct 09 PHP
社区(php&amp;&amp;mysql)三
Oct 09 PHP
PHP4实际应用经验篇(4)
Oct 09 PHP
PHP 数组实例说明
Aug 18 PHP
php 执行系统命令的方法
Jul 07 PHP
PHP+MySQL 手工注入语句大全 推荐
Oct 30 PHP
thinkPHP使用post方式查询时分页失效的解决方法
Dec 09 PHP
php获取字符串前几位的实例(substr返回字符串的子串用法)
Mar 08 PHP
浅谈thinkphp5 instance 的简单实现
Jul 30 PHP
php之header的不同用法总结(实例讲解)
Nov 28 PHP
PHP实现Huffman编码/解码的示例代码
Apr 20 PHP
Yii2框架配置文件(Application属性)与调试技巧实例分析
May 27 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
PHP开发入门教程之面向对象
2006/12/05 PHP
php shell超强免杀、减少体积工具实现代码
2012/10/16 PHP
php中yii框架实例用法
2020/12/22 PHP
深入理解JavaScript高级之词法作用域和作用域链
2013/12/10 Javascript
刷新页面的几种方法小结(JS,ASP.NET)
2014/01/07 Javascript
详解JavaScript中setSeconds()方法的使用
2015/06/11 Javascript
javascript日期格式化方法汇总
2015/10/04 Javascript
JS日期加减,日期运算代码
2015/11/05 Javascript
理解javascript正则表达式
2016/03/08 Javascript
基于JS实现网页中的选项卡(两种方法)
2017/06/16 Javascript
纯js实现页面返回顶部的动画(超简单)
2017/08/10 Javascript
集成vue到jquery/bootstrap项目的方法
2018/02/10 jQuery
微信小程序实现换肤功能
2018/03/14 Javascript
JSONP原理及应用实例详解
2018/09/13 Javascript
如何在Angular8.0下使用ngx-translate进行国际化配置
2019/07/24 Javascript
layer关闭弹出窗口触发表单提交问题的处理方法
2019/09/25 Javascript
微信小程序canvas开发水果老虎机的思路详解
2020/02/07 Javascript
Vue 使用typescript如何优雅的调用swagger API
2020/09/01 Javascript
[01:01]2020完美高校联赛(秋)西安落幕
2021/03/11 DOTA
详解Python的Flask框架中生成SECRET_KEY密钥的方法
2016/06/07 Python
解决Python中字符串和数字拼接报错的方法
2016/10/23 Python
Python3简单实现串口通信的方法
2019/06/12 Python
Python使用pyserial进行串口通信的实例
2019/07/02 Python
Django Form 实时从数据库中获取数据的操作方法
2019/07/25 Python
python实现连续变量最优分箱详解--CART算法
2019/11/22 Python
Python使用requests xpath 并开启多线程爬取西刺代理ip实例
2020/03/06 Python
N:Philanthropy官网:美国洛杉矶基础款服装
2020/06/09 全球购物
新浪网技术部笔试题
2016/08/26 面试题
自我评价的正确写法
2013/09/19 职场文书
应用数学自荐书范文
2013/11/24 职场文书
学校招生宣传广告词
2014/03/19 职场文书
2014财产信托协议书范本
2014/11/18 职场文书
2015年会计人员工作总结
2015/05/22 职场文书
研究生论文答辩开场白
2015/05/27 职场文书
2019新员工心得体会
2019/06/25 职场文书
Mysql Online DDL的使用详解
2021/05/20 MySQL