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查询MySQL大量数据的时候内存占用分析
Jul 22 PHP
php session_start()出错原因分析及解决方法
Oct 28 PHP
PHP加密扩展库Mcrypt安装和实例
Nov 10 PHP
php 批量添加多行文本框textarea一行一个
Jun 03 PHP
php类中的各种拦截器用法分析
Nov 03 PHP
完美实现wordpress禁止文章修订和自动保存的方法
Nov 03 PHP
ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法
Nov 04 PHP
php将远程图片保存到本地服务器的实现代码
Aug 03 PHP
Laravel中注册Facades的步骤详解
Mar 16 PHP
PHP微信企业号开发之回调模式开启与用法示例
Nov 25 PHP
PHP5.5基于mysqli连接MySQL数据库和读取数据操作实例详解
Feb 16 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版(3)
2006/10/09 PHP
生成sessionid和随机密码的例子
2006/10/09 PHP
php 8小时时间差的解决方法小结
2009/12/22 PHP
个人写的PHP验证码生成类分享
2014/08/21 PHP
Laravle eloquent 多对多模型关联实例详解
2017/11/22 PHP
基于laravel Request的所有方法详解
2019/09/29 PHP
xml 与javascript结合的问题解决方法
2007/03/24 Javascript
vs2003 js文件编码问题的解决方法
2010/03/20 Javascript
JQuery中属性过滤选择器用法实例分析
2015/05/18 Javascript
Jquery 1.9.1源码分析系列(十二)之筛选操作
2015/12/02 Javascript
Bootstrap按钮下拉菜单组件详解
2016/05/10 Javascript
js创建对象几种方式的优缺点对比
2016/09/28 Javascript
JavaScript中${pageContext.request.contextPath}取值问题及解决方案
2016/12/08 Javascript
用 js 的 selection range 操作选择区域内容和图片
2017/04/18 Javascript
JS删除数组里的某个元素方法
2018/02/03 Javascript
Vue 莹石摄像头直播视频实例代码
2018/08/31 Javascript
详解使用jest对vue项目进行单元测试
2018/09/07 Javascript
使用ECharts实现状态区间图
2018/10/25 Javascript
vue使用Proxy实现双向绑定的方法示例
2019/03/20 Javascript
基于vue-cli3+typescript的tsx开发模板搭建过程分享
2020/02/28 Javascript
Python httplib,smtplib使用方法
2008/09/06 Python
跟老齐学Python之玩转字符串(2)
2014/09/14 Python
python实现一次创建多级目录的方法
2015/05/15 Python
Python中random模块生成随机数详解
2016/03/10 Python
Python3中编码与解码之Unicode与bytes的讲解
2019/02/28 Python
Python 3.6 -win64环境安装PIL模块的教程
2019/06/20 Python
python实现发送form-data数据的方法详解
2019/09/27 Python
Python shutil模块用法实例分析
2019/10/02 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
使用tensorboard可视化loss和acc的实例
2020/01/21 Python
PythonPC客户端自动化实现原理(pywinauto)
2020/05/28 Python
使用SQLAlchemy操作数据库表过程解析
2020/06/10 Python
司机职责范本
2014/03/08 职场文书
教师个人自我剖析材料
2014/09/29 职场文书
Django REST framework 限流功能的使用
2021/06/24 Python
Oracle查看表空间使用率以及爆满解决方案详解
2022/07/23 Oracle