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 相关文章推荐
PHP4实际应用经验篇(6)
Oct 09 PHP
PHP实现图片简单上传
Oct 09 PHP
PHP 翻页 实例代码
Aug 07 PHP
PHP中对用户身份认证实现两种方法
Jun 04 PHP
ajax 的post方法实例(带循环)
Jul 04 PHP
php判断GIF图片是否为动画的方法
Sep 04 PHP
php获取当月最后一天函数分享
Feb 02 PHP
php实现表单多按钮提交action的处理方法
Oct 24 PHP
PHP获取input输入框中的值去数据库比较显示出来
Nov 16 PHP
PHP调用接口用post方法传送json数据的实例
May 31 PHP
Yii2框架配置文件(Application属性)与调试技巧实例分析
May 27 PHP
Swoole源码中如何查询Websocket的连接问题详解
Aug 30 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导出到Excel或CSV的详解(附utf8、gbk 编码转换)
2013/06/25 PHP
php检测文件编码的方法示例
2014/04/25 PHP
PHP+MySql+jQuery实现的&quot;顶&quot;和&quot;踩&quot;投票功能
2016/05/21 PHP
PHP微信开发之查询微信精选文章
2016/06/23 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
PHP如何实现阿里云短信sdk灵活应用在项目中的方法
2019/06/14 PHP
Laravel 前端资源配置教程
2019/10/18 PHP
比Jquery的document.ready更快的方法
2010/04/28 Javascript
Js 冒泡事件阻止实现代码
2013/01/27 Javascript
node.js中的path.dirname方法使用说明
2014/12/09 Javascript
浅谈javascript中return语句
2015/07/15 Javascript
js获取css的各种样式并且设置他们的方法
2017/08/22 Javascript
小程序封装路由文件和路由方法(5种全解析)
2019/05/26 Javascript
[27:39]Ti4 循环赛第二日 LGD vs Fnatic
2014/07/11 DOTA
python发送邮件的实例代码(支持html、图片、附件)
2013/03/04 Python
python多线程编程方式分析示例详解
2013/12/06 Python
Python中使用PDB库调试程序
2015/04/05 Python
python+django+sql学生信息管理后台开发
2018/01/11 Python
Series和DataFrame使用简单入门
2019/11/13 Python
Python GUI库PyQt5图形和特效样式QSS介绍
2020/02/25 Python
python调用私有属性的方法总结
2020/07/24 Python
python爬虫请求头设置代码
2020/07/28 Python
HTML5 LocalStorage 本地存储刷新值还在
2017/03/10 HTML / CSS
澳大利亚新奇小玩意网站:Yellow Octopus
2017/12/28 全球购物
美国值得信赖的婚恋交友网站:eHarmony
2018/10/04 全球购物
美国相机和电子产品零售商:Beach Camera
2020/11/26 全球购物
介绍Ibatis的核心类
2013/11/18 面试题
2014年最新领导班子整改方案
2014/09/27 职场文书
2014年圣诞节寄语
2014/12/08 职场文书
红旗渠导游词
2015/02/09 职场文书
2015年检验员工作总结范文
2015/04/30 职场文书
白银帝国观后感
2015/06/17 职场文书
apache基于端口创建虚拟主机的示例
2021/04/24 Servers
浅谈Redis在直播场景的实践方案
2021/04/27 Redis
python实现自动清理文件夹旧文件
2021/05/10 Python
SpringBoot使用AOP实现统计全局接口访问次数详解
2022/06/16 Java/Android