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 安全过滤函数代码
May 07 PHP
Thinkphp搜索时首页分页和搜索页保持条件分页的方法
Dec 05 PHP
php绘制圆形的方法
Jan 24 PHP
php简单实现sql防注入的方法
Apr 22 PHP
详解PHP中cookie和session的区别及cookie和session用法小结
Jun 12 PHP
详解Yii2高级版引入bootstrap.js的一个办法
Mar 21 PHP
解决安装WampServer时提示缺少msvcr110.dll文件的问题
Jul 09 PHP
PHP中OpenSSL加密问题整理
Dec 14 PHP
CI框架附属类用法分析
Dec 26 PHP
PDO::commit讲解
Jan 27 PHP
php扩展开发入门demo示例
Sep 23 PHP
laravel 解决强制跳转 https的问题
Oct 22 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中session变量的销毁
2014/02/27 PHP
php使用cookie保存登录用户名的方法
2015/01/26 PHP
详解WordPress开发中wp_title()函数的用法
2016/01/07 PHP
PHP+Mysql+Ajax实现淘宝客服或阿里旺旺聊天功能(前台页面)
2017/06/16 PHP
PHP精确到毫秒秒杀倒计时实例详解
2019/03/14 PHP
Laravel定时任务的每秒执行代码
2019/10/22 PHP
禁止直接访问php文件代码分享
2020/05/05 PHP
js之WEB开发调试利器:Firebug 下载
2007/01/13 Javascript
用jQuery打造TabPanel效果代码
2010/05/22 Javascript
Jquery中获取iframe的代码
2011/01/11 Javascript
ASP.NET中基于JQUERY的高性能的TreeView补充
2011/02/23 Javascript
jquery中change()用法实例分析
2015/02/06 Javascript
JS敏感词过滤代码
2016/12/23 Javascript
完美解决jQuery的hover事件在IE中不停闪动的问题
2017/02/10 Javascript
vue-router路由简单案例介绍
2017/02/21 Javascript
JS实现的五级联动菜单效果完整实例
2017/02/23 Javascript
详解Weex基于Vue2.0开发模板搭建
2017/03/20 Javascript
Javascript数组及类数组相关原理详解
2020/10/29 Javascript
Python简单调用MySQL存储过程并获得返回值的方法
2015/07/20 Python
在Python的Flask中使用WTForms表单框架的基础教程
2016/06/07 Python
Python使用smtplib模块发送电子邮件的流程详解
2016/06/27 Python
python音频处理用到的操作的示例代码
2017/10/27 Python
python时间日期函数与利用pandas进行时间序列处理详解
2018/03/13 Python
浅析Python函数式编程
2018/10/06 Python
Python3爬虫教程之利用Python实现发送天气预报邮件
2018/12/16 Python
使用 Python 快速实现 HTTP 和 FTP 服务器的方法
2019/07/22 Python
处理python中多线程与多进程中的数据共享问题
2019/07/28 Python
解决pip安装tensorflow中出现的no module named tensorflow.python 问题方法
2021/02/20 Python
纯CSS3打造属于自己的“小黄人”
2016/03/14 HTML / CSS
库存图片、照片、矢量图、视频和音乐:Shutterstock
2021/02/12 全球购物
全球精选男装和家居用品:Article
2020/04/13 全球购物
合伙经营协议书范本
2014/09/13 职场文书
镇党委书记群众路线整改措施思想汇报
2014/10/13 职场文书
社区党风廉政建设调研报告
2015/01/01 职场文书
2015年世界卫生日活动总结
2015/02/09 职场文书
2015年园林绿化工作总结
2015/05/23 职场文书