非常实用的php验证码类


Posted in PHP onMay 15, 2016

本文实例为大家分享了php验证码类,供大家参考,具体内容如下

<?php 
/** 
 * 
 * @author Administrator 
 * 
 */ 
class ValidateCode{ 
   
  private $width; 
  private $height; 
  private $codeNum; 
  private $img_resouce; 
  private $disturbColorNum; 
  private $checkCode; 
   
  function __construct($width=80,$height=20,$codeNum=4) { 
    $this->width=$width; 
    $this->height=$height; 
    $this->codeNum=$codeNum; 
    $this->checkCode=$this->CreateCheckCode(); 
    $number=floor($width*$height/25); 
    if ($number>240-$codeNum) { 
      $this->disturbColorNum=240-$codeNum; 
    }else{ 
      $this->disturbColorNum=$number; 
    } 
  } 
   
  public function showImage($fontpath='') { 
    //创建图像背景 
    $this->Img_resouce(); 
    //var_dump($img_resouce); 
    //设置干扰元素 
    $this->setDistructcolor(); 
    //向图像中随机画出文本 
    $this->outputtext($fontpath); 
    //输出图像 
    $this->outputimage(); 
  } 
  /** 
   * 
   *获取随机创建的验证码 
   */ 
  public function getCheckCode(){ 
     
  } 
  private function Img_resouce(){ 
    //创建一个真彩图像 
    $this->img_resouce=imagecreatetruecolor($this->width,$this->height); 
    //随机设置图像背景 
    $backcolor=imagecolorallocate($this->img_resouce,rand(225,255),rand(225,255),rand(225,255)); 
    //填充颜色 
    imagefill($this->img_resouce, 0, 0, $backcolor); 
    //设置边框背景 
    $border=imagecolorallocate($this->img_resouce, 0,0,0); 
    //画一个矩形 
    imagerectangle($this->img_resouce,0,0,$this->width-1,$this->height-1,$border); 
  } 
  private function setDistructcolor(){ 
    //绘画干扰点 
    for ($i = 0; $i <$this->disturbColorNum; $i++) { 
       
      imagesetpixel($this->img_resouce, rand(1, $this->width-2), rand(1, $this->height-2), rand(0,255)); 
    } 
     
    //绘画干扰线 
    for ($j = 0; $j <3; $j++) { 
      $linecolor=imagecolorallocate($this->img_resouce,rand(0,255),rand(0,255),rand(0,255)); 
      imagearc($this->img_resouce, rand(0,$this->width), rand(0,$this->height), 
       rand(10, 225), rand(20, 150), 
       55, 44, $linecolor); 
    } 
  } 
  private function CreateCheckCode(){ 
    $code='23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ'; 
    $string=''; 
    for ($i = 0; $i < $this->codeNum; $i++) { 
       
      $char=$code{rand(0, strlen($code)-1)}; 
      $string.=$char; 
    } 
    return $string; 
  } 
  private function outputtext($fontpath=''){ 
    for ($i = 0; $i < $this->codeNum; $i++) { 
      $fontcolor=imagecolorallocate($this->img_resouce, rand(0,128), rand(0, 128), rand(0, 128)); 
      if ($fontpath=='') { 
         
         $fontsize=rand(3, 5); 
         $x=floor($this->width/$this->codeNum)*$i+3; 
         $y=rand(0, $this->height-20); 
         imagechar($this->img_resouce, $fontsize, $x, $y, $this->checkCode{$i}, $fontcolor); 
    }else{ 
         $fontsize=rand(12, 16); 
         $x=floor(($this->width-8)/$this->codeNum)*$i+8; 
         $y=rand($fontsize, $this->height-15); 
         imagettftext($this->img_resouce,$fontsize,rand(-45,45),$x,$y,$fontcolor,fontpath,$this->checkCode{$i}); 
       } 
    } 
  } 
  private function outputimage() { 
     
    if (imagetypes() & IMG_GIF) { 
      header("Content-type: image/gif"); 
      imagegif($this->img_resouce); 
    }else if(imagetypes() & IMG_JPEG) { 
      header("Content-type: image/jpeg"); 
      imagejpeg($this->img_resouce); 
    }else if(imagetypes() & IMG_PNG) { 
      header("Content-type: image/png"); 
      imagepng($this->img_resouce); 
    }else { 
      echo "PHP不支持的类型"; 
    } 
     
     
  } 
  private function __destruct(){ 
     
    imagedestroy($this->img_resouce); 
  } 
} 
?>

以上就是本文的全部内容,希望对大家的学习有所帮助。

PHP 相关文章推荐
php 中文处理函数集合
Aug 27 PHP
PHP源码之 ext/mysql扩展部分
Jul 17 PHP
phpMyAdmin链接MySql错误 个人解决方案
Dec 28 PHP
PHP中static关键字原理的学习研究分析
Jul 18 PHP
仿Aspnetpager的一个PHP分页类代码 附源码下载
Oct 08 PHP
php 购物车完整实现代码
Jun 05 PHP
Codeigniter+PHPExcel实现导出数据到Excel文件
Jun 12 PHP
php通过sort()函数给数组排序的方法
Mar 18 PHP
thinkphp命名空间用法实例详解
Dec 30 PHP
PHP使用token防止表单重复提交的方法
Apr 07 PHP
thinkPHP简单实现多个子查询语句的方法
Dec 05 PHP
基于php双引号中访问数组元素报错的解决方法
Feb 01 PHP
thinkphp框架下404页面设置 仅三步
May 14 #PHP
php基于CodeIgniter实现图片上传、剪切功能
May 14 #PHP
PHP单例模式是什么 php实现单例模式的方法
May 14 #PHP
PHP pear安装配置教程
May 14 #PHP
php+html5+ajax实现上传图片的方法
May 14 #PHP
yii2使用ajax返回json的实现方法
May 14 #PHP
php文件上传类完整实例
May 14 #PHP
You might like
PHP删除数组中的特定元素的代码
2012/06/28 PHP
php实现压缩多个CSS与JS文件的方法
2014/11/11 PHP
javascript页面渲染速度测试脚本分享
2014/04/15 Javascript
JavaScript判断字符长度、数字、Email、电话等常用判断函数分享
2015/04/01 Javascript
JQuery导航菜单选择特效
2016/04/11 Javascript
AngularJS实用基础知识_入门必备篇(推荐)
2017/07/10 Javascript
微信小程序获取手机网络状态的方法【附源码下载】
2017/12/08 Javascript
详解如何用模块化的方式写vuejs
2017/12/16 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
Vue实现点击时间获取时间段查询功能
2020/08/21 Javascript
浅析Vue.js 中的条件渲染指令
2018/11/19 Javascript
原生js实现日期选择插件
2020/05/21 Javascript
vue实现前端分页完整代码
2020/06/17 Javascript
[53:36]Liquid vs VP Supermajor决赛 BO 第三场 6.10
2018/07/05 DOTA
跟老齐学Python之集合(set)
2014/09/24 Python
Python自动重试HTTP连接装饰器
2015/04/28 Python
python利用rsa库做公钥解密的方法教程
2017/12/10 Python
python tensorflow基于cnn实现手写数字识别
2018/01/01 Python
python 将字符串转换成字典dict的各种方式总结
2018/03/23 Python
pycharm 主题theme设置调整仿sublime的方法
2018/05/23 Python
Python连接Mssql基础教程之Python库pymssql
2018/09/16 Python
Python3使用TCP编写一个简易的文件下载器功能
2019/05/08 Python
Python测试模块doctest使用解析
2019/08/10 Python
Python类中的装饰器在当前类中的声明与调用详解
2020/04/15 Python
Pandas中DataFrame基本函数整理(小结)
2020/07/20 Python
python 模块导入问题汇总
2021/02/01 Python
用CSS3和table标签实现一个圆形轨迹的动画的示例代码
2019/01/17 HTML / CSS
中西医结合临床医学专业大学生自荐信
2013/09/28 职场文书
大学生毕业的自我鉴定
2013/11/13 职场文书
医学院学生的自我评价分享
2013/11/19 职场文书
第一批党的群众路线教育实践活动工作总结
2014/03/03 职场文书
师德模范事迹材料
2014/06/03 职场文书
党的群众路线教育实践活动个人对照检查材料(公安)
2014/11/05 职场文书
银行求职自荐信范文
2015/03/04 职场文书
银行保安拾金不昧表扬稿
2015/05/05 职场文书
oracle DGMGRL ORA-16603报错的解决方法(DG Broker)
2021/04/06 Oracle