非常实用的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(5) 类和对象
Feb 16 PHP
判断是否为指定长度内字符串的php函数
Feb 16 PHP
PHP实现使用优酷土豆视频地址获取swf播放器分享地址
Jun 05 PHP
php修改指定文件后缀的方法
Sep 11 PHP
PHP利用APC模块实现文件上传进度条的方法
Jan 26 PHP
php获取当月最后一天函数分享
Feb 02 PHP
php对二维数组进行相关操作(排序、转换、去空白等)
Nov 04 PHP
PHP如何使用Memcached
Apr 05 PHP
yii框架搜索分页modle写法
Dec 19 PHP
关于php支持的协议与封装协议总结(推荐)
Nov 17 PHP
php实现多站点共用session实现单点登录的方法详解
Sep 18 PHP
TP5框架实现上传多张图片的方法分析
Mar 29 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
星际争霸教主Flash的ID由来:你永远不会知道他之前的ID是www!
2019/01/18 星际争霸
PHP中常用的输出函数总结
2014/09/22 PHP
一个比较不错的PHP日历类分享
2014/11/18 PHP
php+mysql实现简单的增删改查功能
2015/07/13 PHP
PHP使用mysql与mysqli连接Mysql数据库用法示例
2016/07/07 PHP
php函数式编程简单示例
2019/08/08 PHP
js实现翻页后保持checkbox选中状态的实现方法
2012/11/03 Javascript
原创jQuery弹出层插件分享
2015/04/02 Javascript
jQuery实现大转盘抽奖活动仿QQ音乐代码分享
2015/08/21 Javascript
ANGULARJS中使用JQUERY分页控件
2015/09/16 Javascript
js实现手机拍照上传功能
2017/01/17 Javascript
webpack打包后直接访问页面图片路径错误的解决方法
2017/06/17 Javascript
原生js二级联动效果
2017/06/20 Javascript
简单谈谈vue的过渡动画(推荐)
2017/10/11 Javascript
AngularJS实现的省市二级联动功能示例【可对选项实现增删】
2017/10/26 Javascript
React/Redux应用使用Async/Await的方法
2017/11/16 Javascript
vue移动端城市三级联动组件使用详解
2019/07/26 Javascript
[04:26]2014DOTA2国际邀请赛-Newbee顺利进入胜者组决赛 独家专访战神7
2014/07/19 DOTA
[00:50]2014DOTA2国际邀请赛 NEWBEE战队回顾
2014/08/01 DOTA
[03:27]《辉夜杯》线下训练营 导师CU和海涛指点迷津
2015/10/23 DOTA
python安装cx_Oracle模块常见问题与解决方法
2017/02/21 Python
浅谈python中的数字类型与处理工具
2017/08/02 Python
python操作docx写入内容,并控制文本的字体颜色
2020/02/13 Python
Python如何读取、写入JSON数据
2020/07/28 Python
python 写一个水果忍者游戏
2021/01/13 Python
详解px单位html5响应式方案
2018/03/08 HTML / CSS
台湾团购、宅配和优惠券:17Life
2017/08/14 全球购物
澳大利亚购买太阳镜和眼镜网站:Glamoureyes
2020/09/22 全球购物
小学生班会演讲稿
2014/01/09 职场文书
优秀求职信范文分享
2014/01/26 职场文书
2014年教师节寄语
2014/04/03 职场文书
教师党的群众路线对照检查材料
2014/09/24 职场文书
乡镇民主生活会发言材料
2014/10/20 职场文书
人身损害赔偿协议书
2016/03/22 职场文书
Python3使用Qt5来实现简易的五子棋小游戏
2022/05/02 Python
Vite + React从零开始搭建一个开源组件库
2022/06/25 Javascript