非常实用的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与MySQL交互使用详解
Oct 09 PHP
PHP详细彻底学习Smarty
Mar 27 PHP
PHP控制网页过期时间的代码
Sep 28 PHP
php strrpos()与strripos()函数
Aug 31 PHP
destoon复制新模块的方法
Jun 21 PHP
php禁止直接从浏览器输入地址访问.php文件的方法
Nov 04 PHP
php将csv文件导入到mysql数据库的方法
Dec 24 PHP
33道php常见面试题及答案
Jul 06 PHP
PHP中使用substr()截取字符串出现中文乱码问题该怎么办
Oct 21 PHP
Joomla语言翻译类Jtext用法分析
May 05 PHP
PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
Sep 11 PHP
Smarty模板语法详解
Jul 20 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实现将GB编码转换为UTF8
2006/11/25 PHP
php ci框架中加载css和js文件失败的解决方法
2014/03/03 PHP
Thinkphp框架开发移动端接口(2)
2016/08/18 PHP
thinkPHP微信分享接口JSSDK用法实例
2017/07/07 PHP
javascript错误的认识不用关心内存管理
2012/12/15 Javascript
浅析JavaScript原型继承的陷阱
2013/12/03 Javascript
详解JavaScript数组的操作大全
2015/10/19 Javascript
用JavaScript动态建立或增加CSS样式表的实现方法
2016/05/20 Javascript
JS及PHP代码编写八大排序算法
2016/07/12 Javascript
基于angularJS的表单验证指令介绍
2016/10/21 Javascript
js实现截图保存图片功能的代码示例
2017/02/16 Javascript
微信小程序 动态的设置图片的高度和宽度详解及实例代码
2017/02/24 Javascript
node.js中EJS 模板快速入门教程
2017/05/08 Javascript
Nodejs实现文件上传的示例代码
2017/09/26 NodeJs
Vue项目中添加锁屏功能实现思路
2018/06/29 Javascript
小程序实现抽奖动画
2020/04/16 Javascript
Vue+Element实现表格编辑、删除、以及新增行的最优方法
2019/05/28 Javascript
vue实现PC端录音功能的实例代码
2019/06/05 Javascript
Vue.js 实现地址管理页面思路详解(地址添加、编辑、删除和设置默认地址)
2019/12/11 Javascript
vue路由切换时取消之前的所有请求操作
2020/09/01 Javascript
Python写的创建文件夹自定义函数mkdir()
2014/08/25 Python
在Python中使用matplotlib模块绘制数据图的示例
2015/05/04 Python
python实现机器学习之元线性回归
2018/09/06 Python
python制作图片缩略图
2019/04/30 Python
python中的colorlog库使用详解
2019/07/05 Python
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
2019/09/16 Python
基于html5实现的图片墙效果
2014/10/16 HTML / CSS
Jones New York官网:美国女装品牌,受白领女性欢迎
2019/11/26 全球购物
sort命令的作用和用法
2013/08/25 面试题
Static Nested Class 和 Inner Class的不同
2013/11/28 面试题
物控部经理职务说明书
2014/02/25 职场文书
教师对学生的寄语
2014/04/03 职场文书
小学生2014国庆节演讲稿:祖国在我心中
2014/09/21 职场文书
党小组鉴定意见
2015/06/02 职场文书
中秋节祝酒词
2015/08/12 职场文书
《追风筝的人》:人心中的成见是座大山,但请不忘初心
2019/11/15 职场文书