非常实用的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的header和asp中的redirect比较
Oct 09 PHP
PHP 简单日历实现代码
Oct 28 PHP
php中批量修改文件后缀名的函数代码
Oct 23 PHP
PHP三元运算的2种写法代码实例
May 12 PHP
PHP命名空间(namespace)的使用基础及示例
Aug 18 PHP
php将图片文件转换成二进制输出的方法
Jun 10 PHP
PHP XML和数组互相转换详解
Oct 26 PHP
PHP守护进程化在C和PHP环境下的实现
Nov 21 PHP
PHP面向对象程序设计之多态性的应用示例
Dec 19 PHP
php使用fullcalendar日历插件详解
Mar 06 PHP
Yii框架操作cookie与session的方法实例详解
Sep 04 PHP
PHP连接MySQL数据库三种实现方法
Dec 10 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
中英文字符串翻转函数
2008/12/09 PHP
合并ThinkPHP配置文件以消除代码冗余的实现方法
2014/07/22 PHP
PHP getallheaders无法获取自定义头(headers)的问题
2016/03/23 PHP
PHP开发api接口安全验证操作实例详解
2020/03/26 PHP
jQuery 使用手册(一)
2009/09/23 Javascript
js和jquery批量绑定事件传参数一(新猪猪原创)
2010/06/23 Javascript
Js制作简单弹出层DIV在页面居中 中间显示遮罩的具体方法
2013/08/08 Javascript
JS实现鼠标经过好友列表中的好友头像时显示资料卡的效果
2014/07/02 Javascript
javascript中基本类型和引用类型的区别分析
2015/05/12 Javascript
jQuery对JSON数据进行排序输出的方法
2015/06/24 Javascript
使用AmplifyJS组件配合JavaScript进行编程的指南
2015/07/28 Javascript
javascript中闭包(Closure)详解
2016/01/06 Javascript
JQuery点击事件回到页面顶部效果的实现代码
2016/05/24 Javascript
SpringMVC+bootstrap table实例详解
2017/06/02 Javascript
JS使用tofixed与round处理数据四舍五入的区别
2017/10/25 Javascript
vue中eventbus被多次触发以及踩过的坑
2017/12/02 Javascript
Angular模版驱动表单的使用总结
2018/05/05 Javascript
深入浅析AngularJs模版与v-bind
2018/07/06 Javascript
vue实现百度下拉列表交互操作示例
2019/03/12 Javascript
package.json配置文件构成详解
2019/08/27 Javascript
关于AngularJS中几种Providers的区别总结
2020/05/17 Javascript
初步解析Python下的多进程编程
2015/04/28 Python
python中获得当前目录和上级目录的实现方法
2017/10/12 Python
Python @property使用方法解析
2019/09/17 Python
python requests模拟登陆github的实现方法
2019/12/26 Python
python对指定字符串逆序的6种方法(小结)
2020/04/02 Python
使用Python实现微信拍一拍功能的思路代码
2020/07/09 Python
Ray-Ban雷朋瑞典官方网站:全球领先的太阳眼镜品牌
2019/08/22 全球购物
介绍一下Linux中的链接
2016/06/05 面试题
Servlet的生命周期
2013/08/25 面试题
高中毕业生个人自我鉴定
2013/11/24 职场文书
安全资料员岗位职责
2013/12/14 职场文书
乡镇组织委员个人整改措施
2014/09/16 职场文书
党的群众路线教育实践活动心得体会(医院)
2014/11/03 职场文书
讲文明倡议书
2015/04/29 职场文书
MySQL数据库完全卸载的方法
2022/03/03 MySQL