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 相关文章推荐
在smarty模板中使用PHP函数的方法
Apr 23 PHP
深入理解用mysql_fetch_row()以数组的形式返回查询结果
Jun 05 PHP
解析Linux下Varnish缓存的配置优化
Jun 20 PHP
PHP IE中下载附件问题解决方法
Jan 07 PHP
php面象对象数据库操作类实例
Dec 02 PHP
一个完整的PHP类包含的七种语法说明
Jun 04 PHP
PHP json_encode() 函数详解及中文乱码问题
Nov 05 PHP
PHP常用工具类大全附全部代码下载
Dec 07 PHP
ThinkPHP路由机制简介
Mar 23 PHP
Yii模型操作之criteria查找数据库的方法
Jul 15 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
Nov 12 PHP
基于Laravel 多个中间件的执行顺序详解
Oct 21 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中的替代语法介绍
2015/01/09 PHP
php实现在多维数组中查找特定value的方法
2015/07/29 PHP
Yii2实现同时搜索多个字段的方法
2016/08/10 PHP
Thinkphp框架开发移动端接口(1)
2016/08/18 PHP
JavaScript语言中的Literal Syntax特性分析
2007/03/08 Javascript
最常用的12种设计模式小结
2011/08/09 Javascript
JavaScript 动态加载脚本和样式的方法
2015/04/13 Javascript
标准的js无缝滚动效果
2016/08/30 Javascript
Vuejs第九篇之组件作用域及props数据传递实例详解
2016/09/05 Javascript
微信小程序 WebSocket详解及应用
2017/01/21 Javascript
js中DOM三级列表(代码分享)
2017/03/20 Javascript
使用Vue开发一个实时性时间转换指令
2018/01/17 Javascript
vue通过点击事件读取音频文件的方法
2018/05/30 Javascript
Angular CLI 使用教程指南参考小结
2019/04/10 Javascript
elementUi vue el-radio 监听选中变化的实例代码
2019/06/28 Javascript
JS快速实现简单计算器
2020/04/08 Javascript
jquery简易手风琴插件的封装
2020/10/13 jQuery
[03:30]DOTA2完美“圣”典精彩集锦
2016/12/27 DOTA
Python2.x版本中基本的中文编码问题解决
2015/10/12 Python
python学习——内置函数、数据结构、标准库的技巧(推荐)
2019/04/18 Python
python 整数越界问题详解
2019/06/27 Python
详解Django定时任务模块设计与实践
2019/07/24 Python
python 正则表达式参数替换实例详解
2020/01/17 Python
tensorflow 固定部分参数训练,只训练部分参数的实例
2020/01/20 Python
Django用户身份验证完成示例代码
2020/04/03 Python
面向新手解析python Beautiful Soup基本用法
2020/07/11 Python
Python 利用flask搭建一个共享服务器的步骤
2020/12/05 Python
澳大利亚足球鞋和服装购物网站:Ultra Football
2018/10/11 全球购物
毕业生的自我鉴定
2013/10/29 职场文书
电厂职工自我鉴定
2014/02/20 职场文书
硕士生工作推荐信
2014/03/07 职场文书
cf战队收人广告词
2014/03/14 职场文书
有子女的离婚协议书怎么写(范本)
2014/09/29 职场文书
解除同居协议书
2015/01/29 职场文书
涨价通知怎么写
2015/04/23 职场文书
2016年先进教师个人事迹材料
2016/02/26 职场文书