PHP实现适用于自定义的验证码类


Posted in PHP onJune 15, 2016

本文实例为大家分享了PHP验证码类,利用对象来实现的验证码类,供大家参考,具体内容如下

<?php
 
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
Class Image{
  
 private $img;
 public $width = 85;
 public $height = 25;
 public $code;
 public $code_len = 4;
 public $code_str = "329832983DSDSKDSLKQWEWQ2lkfDSFSDjfdsfdsjwlkfj93290KFDSKJFDSOIDSLK";
 public $bg_color = '#DCDCDC';
 public $font_size = 16;
 public $font = 'font.ttf';
 public $font_color = '#000000';
  
 //创建验证码饿字符创
 public function create_code(){
  $code = '';
  for( $i=0;$i<$this->code_len;$i++ ){
   $code .= $this->code_str[mt_rand(0, strlen($this->code_str)-1)];
 }
  return $this->code = $code;
 }
  
 //输出图像
 public function getImage(){
  $w = $this->width;
  $h = $this->height;
  $bg_color = $this->bg_color;
  $img = imagecreatetruecolor($w, $h);
  $bg_color = imagecolorallocate($img, 
 hexdec(substr($bg_color, 1,2)), hexdec(substr($bg_color, 3,2)), hexdec(substr($bg_color, 5,2)));
 imagefill($img, 0, 0, $bg_color);
  $this->img = $img;
  $this->create_font();
  $this->create_pix();
 $this->show_code();
 }
 
 
 //写入验证码
 public function create_font(){
  $this->create_code();
  $color = $this->font_color;
  $font_color = imagecolorallocate($this->img, hexdec(substr($color,1,2)), hexdec(substr($color, 3,2)), hexdec(substr($color,5,2)));
  $x = $this->width/$this->code_len;
  for( $i=0;$i<$this->code_len;$i++ ){
   $txt_color = imagecolorallocate($this->img, mt_rand(0,100), mt_rand(0, 150), mt_rand(0, 200));
   imagettftext($this->img, $this->font_size, mt_rand(-30, 30), $x*$i+mt_rand(3, 6), mt_rand($this->height/1.2, $this->height), $txt_color, $this->font , $this->code[$i]); 
   //imagestring($this->img, $this->font_size, $x*$i+mt_rand(3, 6),mt_rand(0, $this->height/4) , $this->code[$i], $font_color);
  }
  $this->font_color = $font_color;
 }
  
 //画干扰线
 public function create_pix(){
  $pix_color= $this->font_color;
  for($i=0;$i<100;$i++){
   imagesetpixel($this->img, mt_rand(0, $this->width),mt_rand(0, $this->height), $pix_color);
  }
  for($j=0;$j<4;$j++){
   imagesetthickness($this->img, mt_rand(1, 2));
   imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
  }
 }
  
 //得到验证码
 public function getCode(){
  return strtoupper($this->code);
 }
 
 
 //输出验证码
 private function show_code(){
  header("Content-type:image/png");
  imagepng($this->img);
  imagedestroy($this->img);
 }
}

效果图:

PHP实现适用于自定义的验证码类

精彩专题分享:ASP.NET验证码大全 PHP验证码大全 java验证码大全

以上就是使用对象编写的验证码类的全部内容,希望对大家学习PHP程序设计有所帮助。

PHP 相关文章推荐
PHP5 安装方法
Oct 09 PHP
一个php作的文本留言本的例子(六)
Oct 09 PHP
MySQL授权问题总结
May 06 PHP
php中对xml读取的相关函数的介绍一
Jun 05 PHP
php 读取shell管道传输过来的内容
Mar 01 PHP
ThinkPHP框架实现session跨域问题的解决方法
Jul 01 PHP
php分割合并两个字符串的函数实例
Jun 19 PHP
Yii2搭建后台并实现rbac权限控制完整实例教程
Apr 28 PHP
Redis使用Eval多个键值自增的操作实例
Nov 04 PHP
PHP针对中英文混合字符串长度判断及截取方法示例
Mar 31 PHP
PHP设计模式(六)桥连模式Bridge实例详解【结构型】
May 02 PHP
PHP7 整型处理机制修改
Mar 09 PHP
php实现常见图片格式的水印和缩略图制作(面向对象)
Jun 15 #PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 #PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 #PHP
PHP处理二进制数据的实现方法
Jun 13 #PHP
PHP 在数组中搜索给定的简单实例 array_search 函数
Jun 13 #PHP
phpmailer简单发送邮件的方法(附phpmailer源码下载)
Jun 13 #PHP
PHP array_key_exists检查键名或索引是否存在于数组中的实现方法
Jun 13 #PHP
You might like
PHP 创建文件(文件夹)以及目录操作代码
2010/03/04 PHP
PHP实现自动识别Restful API的返回内容类型
2015/02/07 PHP
thinkPHP订单数字提醒功能的实现方法
2016/12/01 PHP
[原创]PHPCMS遭遇会员投稿审核无效的解决方法
2017/01/11 PHP
QQ登录简单实现代码
2021/03/09 Javascript
js获取url参数的使用扩展实例
2007/12/29 Javascript
Jquery中显示隐藏的实现代码分析
2011/07/26 Javascript
利用JS判断用户是否上网(连接网络)
2013/12/23 Javascript
jQuery.extend()、jQuery.fn.extend()扩展方法示例详解
2014/05/08 Javascript
js中的caller和callee属性介绍和例子
2014/06/07 Javascript
DOM节点的替换或修改函数replaceChild()用法实例
2015/01/12 Javascript
JavaScript使用setTimeout实现延迟弹出警告框的方法
2015/04/07 Javascript
Javascript简单实现面向对象编程继承实例代码
2015/11/27 Javascript
基于JavaScript实现快速转换文本语言(繁体中文和简体中文)
2016/03/07 Javascript
判断数组的最佳方法(推荐)
2016/10/11 Javascript
Vue.js实战之通过监听滚动事件实现动态锚点
2017/04/04 Javascript
Vue2路由动画效果的实现代码
2017/07/10 Javascript
详解如何用webpack打包一个网站应用项目
2017/07/12 Javascript
轻松理解vue的双向数据绑定问题
2017/10/30 Javascript
vuejs实现本地数据的筛选分页功能思路详解
2017/11/15 Javascript
NodeJs实现定时任务的示例代码
2017/12/05 NodeJs
微信小程序授权登陆及每次检查是否授权实例代码
2019/09/18 Javascript
js实现滚动条自动滚动
2020/12/13 Javascript
[01:38]女王驾到——至宝魔廷新尊技能&特效展示
2020/06/16 DOTA
python 基于selenium实现鼠标拖拽功能
2020/12/24 Python
公益广告语集锦
2014/03/13 职场文书
房产代理公证处委托书
2014/04/04 职场文书
高中生操行评语
2014/04/25 职场文书
爱情保证书大全
2014/04/29 职场文书
社团活动总结书
2014/06/27 职场文书
师德师风自我评价范文
2014/09/11 职场文书
食品委托检验协议书范本
2014/09/12 职场文书
职工擅自离岗检讨书
2014/09/23 职场文书
公司聚餐通知
2015/04/22 职场文书
《角的初步认识》教学反思
2016/02/17 职场文书
深度学习小工程练习之垃圾分类详解
2021/04/14 Python