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 相关文章推荐
discuz Passport 通行证 整合笔记
Jun 30 PHP
php 图像函数大举例(非原创)
Jun 20 PHP
php cli模式学习(PHP命令行模式)
Jun 03 PHP
解析php时间戳与日期的转换
Jun 06 PHP
探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解
Jun 20 PHP
smarty内部日期函数html_select_date()用法实例分析
Jul 08 PHP
Zend Framework教程之Zend_Db_Table表关联实例详解
Mar 23 PHP
PHP使用token防止表单重复提交的方法
Apr 07 PHP
php加密解密字符串示例
Oct 13 PHP
PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
Sep 17 PHP
OAuth认证协议中的HMACSHA1加密算法(实例)
Oct 25 PHP
PHP SESSION跨页面传递失败解决方案
Dec 11 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数组相加 array(“a”)+array(“b”)结果还是array(“a”)
2012/09/19 PHP
php多维数组去掉重复值示例分享
2014/03/02 PHP
JavaScript获取GridView选择的行内容
2009/04/14 Javascript
jQuery下扩展插件和拓展函数的写法(匿名函数使用的典型例子)
2010/10/20 Javascript
jquery中通过过滤器获取表单元素的实现代码
2011/07/05 Javascript
有关javascript的性能优化 (repaint和reflow)
2013/04/12 Javascript
javascript与cookie 的问题详解
2013/11/11 Javascript
jquery中的查找parents与closest方法之间的区别
2013/12/02 Javascript
jQuery ajax serialize() 方法使用示例
2014/11/02 Javascript
深入探寻seajs的模块化与加载方式
2015/04/14 Javascript
node.js回调函数之阻塞调用与非阻塞调用
2015/11/13 Javascript
js调出上下文菜单的实例
2015/12/17 Javascript
Bootstrap打造一个左侧折叠菜单的系统模板(二)
2016/05/17 Javascript
jQuery实现点击后高亮背景固定显示的菜单效果【附demo源码下载】
2016/09/21 Javascript
jquery submit()不能提交表单的解决方法
2017/04/24 jQuery
angular.js指令中的controller、compile与link函数的不同之处
2017/05/10 Javascript
es6 symbol的实现方法示例
2019/04/02 Javascript
常见的浏览器存储方式(cookie、localStorage、sessionStorage)
2019/05/07 Javascript
Vue Element UI + OSS实现上传文件功能
2019/07/31 Javascript
JS中的模糊查询功能
2019/12/08 Javascript
vue 路由meta 设置导航隐藏与显示功能的示例代码
2020/09/04 Javascript
Python 文件读写操作实例详解
2014/03/12 Python
Pyhton中单行和多行注释的使用方法及规范
2016/10/11 Python
python爬虫入门教程--正则表达式完全指南(五)
2017/05/25 Python
python编写暴力破解zip文档程序的实例讲解
2018/04/24 Python
python tkinter 设置窗口大小不可缩放实例
2020/03/04 Python
python中的selenium安装的步骤(浏览器自动化测试框架)
2020/03/17 Python
python Timer 类使用介绍
2020/12/28 Python
土耳其风格手工珠宝:Ottoman Hands
2019/07/26 全球购物
车队司机自我鉴定
2014/03/02 职场文书
辛亥革命观后感
2015/06/02 职场文书
高中数学课堂教学反思
2016/02/18 职场文书
导游词之徐州-云龙山
2019/09/29 职场文书
Python 循环读取数据内存不足的解决方案
2021/05/25 Python
Spring Boot实战解决高并发数据入库之 Redis 缓存+MySQL 批量入库问题
2022/02/12 Redis
Android基于Fresco实现圆角和圆形图片
2022/04/01 Java/Android