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 相关文章推荐
php下通过伪造http头破解防盗链的代码
Jul 03 PHP
php获取apk包信息的方法
Aug 15 PHP
php获取textarea的值并处理回车换行的方法
Oct 20 PHP
PHP中通过fopen()函数访问远程文件示例
Nov 18 PHP
PHP+Ajax验证码验证用户登录
Jul 20 PHP
PHP编写daemon process详解及实例代码
Sep 30 PHP
Thinkphp整合微信支付功能
Dec 14 PHP
php解析mht文件转换成html的实例
Mar 13 PHP
PHP实现的简单适配器模式示例
Jun 22 PHP
基于php中echo用逗号和用点号的区别详解
Jan 23 PHP
thinkPHP框架实现的简单计算器示例
Dec 07 PHP
PHP多进程简单实例小结
Nov 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
Joomla数据库操作之JFactory::getDBO用法
2016/05/05 PHP
详解PHP防止盗链防止迅雷下载的方法
2017/04/26 PHP
[原创]php实现数组按拼音顺序排序的方法
2017/05/03 PHP
jquery ready()的几种实现方法小结
2010/06/18 Javascript
创建公共调用 jQuery Ajax 带返回值
2012/08/01 Javascript
jquery 获取标签名(tagName)示例代码
2013/07/11 Javascript
屏蔽相应键盘按钮操作
2014/03/10 Javascript
js使用post 方式打开新窗口
2015/02/26 Javascript
JavaScript使用Math.Min返回两个数中较小数的方法
2015/04/06 Javascript
jQuery+html5实现div弹出层并遮罩背景
2015/04/15 Javascript
基于javascript实现九九乘法表
2016/03/27 Javascript
Bootstrap开关(switch)控件学习笔记分享
2016/05/30 Javascript
jQuery实现6位数字密码输入框
2016/12/29 Javascript
js 性能优化之算法和流程控制
2017/02/15 Javascript
jQuery插件echarts实现的循环生成图效果示例【附demo源码下载】
2017/03/04 Javascript
微信小程序页面传值实例分析
2017/04/19 Javascript
微信小程序Redux绑定实例详解
2017/06/07 Javascript
详解Vue 事件修饰符capture 的使用
2017/12/29 Javascript
vue双向绑定数据限制长度的方法
2019/11/04 Javascript
利用js canvas实现五子棋游戏
2020/10/11 Javascript
Python中super关键字用法实例分析
2015/05/28 Python
python结合API实现即时天气信息
2016/01/19 Python
python回调函数中使用多线程的方法
2017/12/25 Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
2018/05/21 Python
Pandas之Fillna填充缺失数据的方法
2019/06/25 Python
Python中利用LSTM模型进行时间序列预测分析的实现
2019/07/26 Python
OpenCV灰度化之后图片为绿色的解决
2020/12/01 Python
浅谈HTML5新增和废弃的标签
2019/04/28 HTML / CSS
美国孕妇装品牌:Destination Maternity
2018/02/04 全球购物
康帕斯酒店预订:Compass Hospitality(支持中文)
2018/08/23 全球购物
给校长的建议书
2014/03/12 职场文书
竞选班长的演讲稿
2014/04/24 职场文书
2014学习优秀共产党员先进事迹思想汇报
2014/09/14 职场文书
农业项目合作意向书
2015/05/08 职场文书
运动员加油词
2015/07/18 职场文书
python pandas 解析(读取、写入)CSV 文件的操作方法
2022/12/24 Python