非常实用的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编程语言开发动态WAP页面
Oct 09 PHP
PHP+.htaccess实现全站静态HTML文件GZIP压缩传输(一)
Feb 15 PHP
IStream与TStream之间的相互转换
Aug 01 PHP
PHP file_get_contents 函数超时的几种解决方法
Jul 30 PHP
php中根据变量的类型 选择echo或dump
Jul 05 PHP
PHP中extract()函数的妙用分析
Jul 11 PHP
浅谈PHP强制类型转换,慎用!
Jun 06 PHP
php is_executable判断给定文件名是否可执行实例
Sep 26 PHP
PHP Socket网络操作类定义与用法示例
Aug 30 PHP
PHP实现对图片的反色处理功能【测试可用】
Feb 01 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
Dec 25 PHP
swoole锁的机制代码实例讲解
Mar 04 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 字符截取 解决中文的截取问题,不用mb系列
2009/09/29 PHP
解析php开发中的中文编码问题
2013/08/08 PHP
使用Composer安装Yii框架的方法
2016/03/15 PHP
Javascript学习笔记2 函数
2010/01/11 Javascript
理解Javascript_12_执行模型浅析
2010/10/18 Javascript
动态创建script在IE中缓存js文件时导致编码的解决方法
2014/05/04 Javascript
jquery获取tagName再进行判断
2014/05/29 Javascript
jQuery判断当前点击的是第几个li的代码
2014/09/26 Javascript
JS实现简单路由器功能的方法
2015/05/27 Javascript
JavaScript函数的一些注意要点小结及js匿名函数
2015/11/10 Javascript
用JavaScript获取页面文档内容的实现代码
2016/06/10 Javascript
JS中定位 position 的使用实例代码
2017/08/06 Javascript
浅谈Vue.js 组件中的v-on绑定自定义事件理解
2017/11/17 Javascript
vue2.0 兄弟组件(平级)通讯的实现代码
2018/01/15 Javascript
Vue用v-for给循环标签自身属性添加属性值的方法
2018/10/18 Javascript
vue3.0 的 Composition API 的使用示例
2020/10/26 Javascript
[01:32]DOTA2次级联赛——首支职业女子战队选拔赛全记录
2014/10/23 DOTA
解析Python中的二进制位运算符
2015/05/13 Python
CentOS 6.X系统下升级Python2.6到Python2.7 的方法
2016/10/12 Python
Python之pandas读写文件乱码的解决方法
2018/04/20 Python
Django Web开发中django-debug-toolbar的配置以及使用
2018/05/06 Python
利用python画出折线图
2018/07/26 Python
Python嵌套式数据结构实例浅析
2019/03/05 Python
Python实战之制作天气查询软件
2019/05/14 Python
django框架model orM使用字典作为参数,保存数据的方法分析
2019/06/24 Python
HTML5 Canvas实现玫瑰曲线和心形图案的代码实例
2014/04/10 HTML / CSS
VICHY薇姿美国官方网站:欧洲药房第一的抗衰老品牌
2017/11/22 全球购物
荷兰时尚精品店:Labels Fashion
2020/03/22 全球购物
怎样有效的进行自我评价
2013/10/06 职场文书
财务总监管理岗位职责
2014/03/08 职场文书
经理任命书模板
2014/06/06 职场文书
财务会计专业求职信
2014/06/09 职场文书
新闻编辑求职信
2014/07/13 职场文书
公安机关正风肃纪剖析材料
2014/10/10 职场文书
大学生毕业个人总结
2015/02/15 职场文书
MySQL入门命令之函数-单行函数-流程控制函数
2021/04/05 MySQL