非常实用的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下MAIL的另一解决方案
Oct 09 PHP
PHP下几种删除目录的方法总结
Aug 19 PHP
PHP中“简单工厂模式”实例代码讲解
Sep 04 PHP
ubuntu10.04配置 nginx+php-fpm模式的详解
Jun 03 PHP
php ios推送(代码)
Jul 01 PHP
PHP实现微信发红包程序
Aug 24 PHP
PHP中PDO连接数据库中各种DNS设置方法小结
May 13 PHP
CI框架中类的自动加载问题分析
Nov 21 PHP
php实现websocket实时消息推送
Mar 30 PHP
PHP设计模式之原型设计模式原理与用法分析
Apr 25 PHP
php微信开发之图片回复功能
Jun 14 PHP
PHP实现redis限制单ip、单用户的访问次数功能示例
Jun 16 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操作xml
2013/10/27 PHP
图解找出PHP配置文件php.ini的路径的方法
2014/08/20 PHP
详解PHP素材图片上传、下载功能
2019/04/12 PHP
用JavaScript页面不刷新时全选择,全删除(GridView)
2009/04/14 Javascript
js 文件引入实现代码
2010/04/23 Javascript
js弹出模式对话框,并接收回传值的方法
2013/03/12 Javascript
javascript中parseInt()函数的定义和用法分析
2014/12/20 Javascript
js显示文本框提示文字的方法
2015/05/07 Javascript
IE下JS保存图片的简单实例
2016/07/15 Javascript
JavaScript数据类型的存储方法详解
2017/08/25 Javascript
vue.js父子组件通信动态绑定的实例
2018/09/28 Javascript
基于JavaScript canvas绘制贝塞尔曲线
2018/12/25 Javascript
vue.js 2.0实现简单分页效果
2019/07/29 Javascript
jquery向后台提交数组的代码分析
2020/02/20 jQuery
jQuery-App输入框实现实时搜索
2020/11/19 jQuery
可用于监控 mysql Master Slave 状态的python代码
2013/02/10 Python
centos 下面安装python2.7 +pip +mysqld
2014/11/18 Python
简单讲解Python中的字符串与字符串的输入输出
2016/03/13 Python
Python实现按当前日期(年、月、日)创建多级目录的方法
2018/04/26 Python
python实现跨excel的工作表sheet之间的复制方法
2018/05/03 Python
python使用knn实现特征向量分类
2018/12/26 Python
详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
2019/02/25 Python
numpy矩阵数值太多不能全部显示的解决
2020/05/14 Python
cookies应对python反爬虫知识点详解
2020/11/25 Python
德国童装购物网站:NICKI´S.com
2018/04/20 全球购物
三陽商会官方网站:Sanyo iStore
2019/05/15 全球购物
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
alice McCALL官网:澳大利亚时尚品牌
2020/11/16 全球购物
大学本科毕业生求职信范文
2013/12/18 职场文书
电焊工岗位职责
2014/03/06 职场文书
大学学生会竞选演讲稿
2014/04/25 职场文书
骨干教师考核方案
2014/05/09 职场文书
幼儿园小班见习报告
2014/10/31 职场文书
2014年人事工作总结范文
2014/11/19 职场文书
2015年工商所工作总结
2015/05/21 职场文书
postgreSQL数据库基础知识介绍
2022/04/12 PostgreSQL