非常实用的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 相关文章推荐
UCenter中的一个可逆加密函数authcode函数代码
Jul 20 PHP
深入phpMyAdmin的安装与配置的详细步骤
May 07 PHP
去除php注释和去除空格函数分享
Mar 13 PHP
php实现的Captcha验证码类实例
Sep 22 PHP
Symfony页面的基本创建实例详解
Jan 26 PHP
php计算指定目录下文件占用空间的方法
Mar 13 PHP
PHP的压缩函数实现:gzencode、gzdeflate和gzcompress的区别
Jan 27 PHP
PHP文件上传处理案例分析
Oct 15 PHP
php mysql实现mysql_select_db选择数据库
Dec 30 PHP
php/JS实现的生成随机密码(验证码)功能示例
Jun 06 PHP
Thinkphp5.0框架使用模型Model的获取器、修改器、软删除数据操作示例
Oct 11 PHP
PHP正则表达式之RCEService回溯
Apr 11 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
初品cakephp 入门基础
2012/02/16 PHP
CodeIgniter配置之routes.php用法实例分析
2016/01/19 PHP
深入理解PHP类的自动载入机制
2016/09/16 PHP
PHP Swoole异步Redis客户端实现方法示例
2019/10/24 PHP
Jquery + Ajax调用webService实例代码(asp.net)
2010/08/27 Javascript
js两行代码按指定格式输出日期时间
2011/10/21 Javascript
jQuery Ajax提交表单查询获得数据实例代码
2012/09/19 Javascript
js单独获取一个checkbox看其是否被选中
2014/09/22 Javascript
javascript 构造函数方式定义对象
2015/01/02 Javascript
每天一篇javascript学习小结(面向对象编程)
2015/11/20 Javascript
js表单验证实例讲解
2016/03/31 Javascript
js 轮播效果实例分享
2016/12/28 Javascript
jQuery实现拖拽可编辑模块功能代码
2017/01/12 Javascript
Vue 过渡(动画)transition组件案例详解
2017/01/22 Javascript
微信小程序scroll-view实现横向滚动和上拉加载示例
2017/03/06 Javascript
原生JS实现自定义滚动条效果
2020/10/27 Javascript
微信小程序自定义多选事件的实现代码
2018/05/17 Javascript
vue多层嵌套路由实例分析
2019/03/19 Javascript
浅谈vue 锚点指令v-anchor的使用
2019/11/13 Javascript
微信小程序吸底区域适配iPhoneX的实现
2020/04/09 Javascript
原生JS利用transform实现banner的无限滚动示例代码
2020/06/15 Javascript
[09:33]2015国际邀请赛第四日TOP10
2015/08/08 DOTA
让代码变得更易维护的7个Python库
2018/10/09 Python
python reverse反转部分数组的实例
2018/12/13 Python
利用python读取YUV文件 转RGB 8bit/10bit通用
2019/12/09 Python
Python常用库大全及简要说明
2020/01/17 Python
python time()的实例用法
2020/11/03 Python
python 实现波浪滤镜特效
2020/12/02 Python
利用python绘制正态分布曲线
2021/01/04 Python
柯基袜:Corgi Socks
2017/01/26 全球购物
美国在线家居装饰店:Belle&June
2018/10/24 全球购物
经典c++面试题五
2014/12/17 面试题
最新教师自我评价分享
2013/11/12 职场文书
2019大学竞选班长发言稿
2019/06/27 职场文书
MongoDB数据库之添删改查
2022/04/26 MongoDB
Vue Element plus使用方法梳理
2022/12/24 Vue.js