非常实用的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 heredoc和phpwind的模板技术使用方法小结
Mar 28 PHP
php打造属于自己的MVC框架
Mar 07 PHP
thinkphp 多表 事务详解
Jun 17 PHP
php获取字段名示例分享
Mar 03 PHP
php的mkdir()函数创建文件夹比较安全的权限设置方法
Jul 28 PHP
CI框架文件上传类及图像处理类用法分析
May 18 PHP
再谈PHP中单双引号的区别详解
Jun 12 PHP
php实现基于openssl的加密解密方法
Sep 30 PHP
php实时倒计时功能实现方法详解
Feb 27 PHP
PHP去除字符串最后一个字符的三种方法实例
Mar 01 PHP
PHP实现动态创建XML文档的方法
Mar 30 PHP
yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】
Apr 13 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 simplexmlElement操作xml的命名空间实现代码
2011/01/04 PHP
php自定义函数call_user_func和call_user_func_array详解
2011/07/14 PHP
使用ThinkPHP自带的Http类下载远程图片到本地的实现代码
2011/08/02 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2014/11/06 PHP
javascript读取xml
2006/11/04 Javascript
js获取客户端网卡的IP地址、MAC地址
2014/03/26 Javascript
JS制作简单的三级联动
2015/03/18 Javascript
JS实现仿Windows7风格的网页右键菜单效果代码
2015/09/11 Javascript
nodejs修复ipa处理过的png图片
2016/02/17 NodeJs
浅析JavaScript函数的调用模式
2016/08/10 Javascript
详解jQuery中的DOM操作
2016/12/23 Javascript
支持移动端原生js轮播图
2017/02/16 Javascript
微信小程序实现图片轮播及文件上传
2017/04/07 Javascript
微信小程序 自定义Toast实例代码
2017/06/12 Javascript
微信小程序checkbox组件使用详解
2018/01/31 Javascript
解决vue项目打包后提示图片文件路径错误的问题
2018/07/04 Javascript
swiper在vue项目中loop循环轮播失效的解决方法
2018/09/15 Javascript
vue基础之v-bind属性、class和style用法分析
2019/03/11 Javascript
js实现拖动缓动效果
2020/01/13 Javascript
[02:17]TI4西雅图DOTA2前线报道 啸天mik夫妻档解说
2014/07/08 DOTA
[01:08:30]DOTA2-DPC中国联赛 正赛 Ehome vs Elephant BO3 第一场 2月28日
2021/03/11 DOTA
python登录豆瓣并发帖的方法
2015/07/08 Python
Python 文件处理注意事项总结
2017/04/10 Python
利用Python爬取微博数据生成词云图片实例代码
2017/08/31 Python
python实现分页效果
2017/10/25 Python
详解python函数传参是传值还是传引用
2018/01/16 Python
tensorflow实现对图片的读取的示例代码
2018/02/12 Python
Python 实现使用dict 创建二维数据、DataFrame
2018/04/13 Python
Python中asyncio与aiohttp入门教程
2018/10/16 Python
利用python GDAL库读写geotiff格式的遥感影像方法
2018/11/29 Python
python分割一个文本为多个文本的方法
2019/07/22 Python
CSS3弹性布局内容对齐(justify-content)属性使用详解
2017/07/31 HTML / CSS
trivago美国:全球最大的酒店价格比较网站
2018/01/18 全球购物
夏尔巴人登珠峰品牌:Sherpa Adventure Gear
2018/02/08 全球购物
开办加工厂创业计划书
2014/01/03 职场文书
双创工作实施方案
2014/03/26 职场文书