非常实用的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 相关文章推荐
杏林同学录(五)
Oct 09 PHP
第4章 数据处理-php字符串的处理-郑阿奇(续)
Jul 04 PHP
编译php 5.2.14+fpm+memcached(具体操作详解)
Jun 18 PHP
跟我学Laravel之快速入门
Oct 15 PHP
php实现的通用图片处理类
Mar 24 PHP
在Mac OS上编译安装Nginx+PHP+MariaDB开发环境的教程
Feb 23 PHP
PHP实现的DES加密解密实例代码
Apr 06 PHP
PHP文件上传处理案例分析
Oct 15 PHP
注释PHP和html混合代码的小技巧(分享)
Nov 03 PHP
phpStudy配置多站点多域名方法及遇到的403错误解决方法
Oct 19 PHP
PHP实现生成模糊图片的方法示例
Dec 21 PHP
PHP使用函数用法详解
Sep 30 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 getcwd与dirname(__FILE__)区别详解
2016/09/24 PHP
基于php判断客户端类型
2016/10/14 PHP
php面向对象程序设计入门教程
2019/06/22 PHP
某页码显示的helper 少量调整,另附js版
2010/09/12 Javascript
将string解析为json的几种方式小结
2010/11/11 Javascript
网页中可关闭的漂浮窗口实现可自行调节
2013/08/20 Javascript
经过绑定元素时会多次触发mouseover和mouseout事件
2014/02/28 Javascript
自己封装的常用javascript函数分享
2015/01/07 Javascript
nodejs中实现sleep功能实例
2015/03/24 NodeJs
原生js实现模拟滚动条
2015/06/15 Javascript
基于jquery实现鼠标左右拖动滑块滑动附源码下载
2015/12/23 Javascript
实例讲解JavaScript中instanceof运算符的用法
2016/06/08 Javascript
AngularJS实现表单验证功能
2017/01/09 Javascript
@ResponseBody 和 @RequestBody 注解的区别
2017/03/08 Javascript
JavaScript登录记住密码操作(超简单代码)
2017/03/22 Javascript
Node.js Mongodb 密码特殊字符 @的解决方法
2017/04/11 Javascript
使用Vue写一个datepicker的示例
2018/01/27 Javascript
vue和react等项目中更简单的实现展开收起更多等效果示例
2018/02/22 Javascript
微信小程序中遇到的iOS兼容性问题小结
2018/11/14 Javascript
JS散列表碰撞处理、开链法、HashTable散列示例
2019/02/08 Javascript
vue键盘事件点击事件加native操作
2020/07/27 Javascript
vue 全局封装loading加载教程(全局监听)
2020/11/05 Javascript
利用Python2下载单张图片与爬取网页图片实例代码
2017/12/25 Python
python之django母板页面的使用
2018/07/03 Python
python直接获取API传递回来的参数方法
2018/12/17 Python
Python使用pyshp库读取shapefile信息的方法
2018/12/29 Python
PyCharm永久激活方式(推荐)
2020/09/22 Python
解决python 执行sql语句时所传参数含有单引号的问题
2020/06/06 Python
CSS中垂直居中的简单实现方法
2015/07/06 HTML / CSS
轻松制作精彩视频:Animoto
2018/09/19 全球购物
中国央视网签名寄语
2014/01/18 职场文书
毕业生自荐信格式
2014/03/07 职场文书
爱耳日活动总结
2014/04/30 职场文书
2016校本研修培训心得体会
2016/01/08 职场文书
Python中使用ipython的详细教程
2021/06/22 Python
python套接字socket通信
2022/04/01 Python