非常实用的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 相关文章推荐
mysql_num_rows VS COUNT 效率问题分析
Apr 23 PHP
phpmyadmin提示The mbstring extension is missing的解决方法
Dec 17 PHP
php实现统计网站在线人数的方法
May 12 PHP
php实现的简单美国商品税计算函数
Jul 13 PHP
PHP实现HTML页面静态化的方法
Nov 04 PHP
学习php设计模式 php实现建造者模式
Dec 07 PHP
Yii2增删改查之查询 where参数详细介绍
Aug 08 PHP
PHP实现的方程求解示例分析
Nov 11 PHP
php断点续传之文件分割合并详解
Dec 13 PHP
PHP+mysql实现的三级联动菜单功能示例
Feb 15 PHP
PHP检测一个数组有没有定义的方法步骤
Jul 20 PHP
Yii框架响应组件用法实例分析
Sep 04 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 curl或file_get_contents下载图片损坏或无法打开的问题
2019/10/11 PHP
JS 非图片动态loading效果实现代码
2010/04/09 Javascript
jquery的Theme和Theme Switcher使用小结
2010/09/08 Javascript
js取消单选按钮选中并判断对象是否为空
2013/11/14 Javascript
JS获取及设置TextArea或input文本框选择文本位置的方法
2015/03/24 Javascript
jQuery往返城市和日期查询实例讲解
2015/10/09 Javascript
js获取鼠标位置实例详解
2015/12/09 Javascript
confirm确认对话框的实现方法总结
2016/06/17 Javascript
Vue 2.0 服务端渲染入门介绍
2017/03/29 Javascript
vue使用Axios做ajax请求详解
2017/06/07 Javascript
使用 vue-i18n 切换中英文效果
2018/05/23 Javascript
Vue-Router的使用方法
2018/09/05 Javascript
利用d3.js实现蜂巢图表带动画效果
2019/09/03 Javascript
vue-cli创建的项目中的gitHooks原理解析
2020/02/14 Javascript
jquery向后台提交数组的代码分析
2020/02/20 jQuery
快速解决Vue、element-ui的resetFields()方法重置表单无效的问题
2020/08/12 Javascript
深度剖析使用python抓取网页正文的源码
2014/06/11 Python
Python中is和==的区别详解
2018/11/15 Python
Python Django 添加首页尾页上一页下一页代码实例
2019/08/21 Python
python中sort和sorted排序的实例方法
2019/08/26 Python
python使用多线程+socket实现端口扫描
2020/05/28 Python
pytorch 多分类问题,计算百分比操作
2020/07/09 Python
HTML5标签与HTML4标签的区别示例介绍
2013/07/18 HTML / CSS
html+js 实现markdown编辑器效果
2019/10/23 HTML / CSS
Html5 new XMLHttpRequest()监听附件上传进度
2021/01/14 HTML / CSS
销售自荐信
2013/10/22 职场文书
知识竞赛主持词
2014/03/26 职场文书
新春寄语大全
2014/04/09 职场文书
社区健康教育工作方案
2014/06/03 职场文书
八一建军节营销活动方案
2014/08/31 职场文书
司机工作自我鉴定
2014/09/19 职场文书
个人债务授权委托书
2014/10/17 职场文书
个人工作年终总结
2015/03/09 职场文书
2016年记者节感言
2015/12/08 职场文书
Java后台生成图片的完整步骤
2021/08/04 Java/Android
关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)
2022/04/04 Python