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 相关文章推荐
几种显示数据的方法的比较
Oct 09 PHP
php 获取mysql数据库信息代码
Mar 12 PHP
探讨fckeditor在Php中的配置详解
Jun 08 PHP
处理单名多值表单的详解
Jun 08 PHP
php删除数组元素示例分享
Feb 17 PHP
php环境套包 dedeampz 伪静态设置示例
Mar 26 PHP
ThinkPHP 3.2 版本升级了哪些内容
Mar 05 PHP
PHP读取PPT文件的方法
Dec 10 PHP
PHP 二维数组和三维数组的过滤
Mar 16 PHP
PHP is_array() 检测变量是否是数组的实现方法
Jun 13 PHP
设定php简写功能的方法
Nov 28 PHP
ThinkPHP5.1+Ajax实现的无刷新分页功能示例
Feb 10 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/12/19 PHP
使用PHP如何实现高效安全的ftp服务器(二)
2015/12/30 PHP
PHP支付系统设计与典型案例分享
2016/08/02 PHP
Aster vs Newbee BO5 第三场2.19
2021/03/10 DOTA
prototype 的说明 js类
2006/09/07 Javascript
如何用javascript判断录入的日期是否合法
2007/01/08 Javascript
javascript 数组排序函数sort和reverse使用介绍
2013/11/21 Javascript
8个实用的jQuery技巧
2014/03/04 Javascript
javascript使用window.open提示“已经计划系统关机”的原因
2014/08/15 Javascript
node.js中的fs.utimesSync方法使用说明
2014/12/15 Javascript
js实现ifram取父窗口URL地址的方法
2015/02/09 Javascript
JavaScript 匿名函数和闭包介绍
2015/04/13 Javascript
Bootstrap与KnockoutJs相结合实现分页效果实例详解
2016/05/03 Javascript
前端设计师们最常用的JS代码汇总
2016/09/25 Javascript
JS DOMReady事件的六种实现方法总结
2016/11/23 Javascript
Vue.js -- 过滤器使用总结
2017/02/18 Javascript
Vue组件化通讯的实例代码
2017/06/23 Javascript
ES6新增数据结构WeakSet的用法详解
2017/08/07 Javascript
Vue keep-alive实践总结(推荐)
2017/08/31 Javascript
微信小程序scroll-view锚点链接滚动跳转功能
2019/12/12 Javascript
js实现小星星游戏
2020/03/23 Javascript
[03:40]DOTA2亚洲邀请赛小组赛第二日 赛事回顾
2015/01/31 DOTA
python使用fileinput模块实现逐行读取文件的方法
2015/04/29 Python
Django原生sql也能使用Paginator分页的示例代码
2017/11/15 Python
PyQt5每天必学之工具提示功能
2018/04/19 Python
python 遍历目录(包括子目录)下所有文件的实例
2018/07/11 Python
Python中文编码知识点
2019/02/18 Python
MADE法国:提供原创设计师家具
2018/09/18 全球购物
澳大利亚珍珠首饰购物网站:Vayo Pearls
2019/03/11 全球购物
Sperry澳大利亚官网:源自美国帆船鞋创始品牌
2019/07/29 全球购物
房屋登记授权委托书范本
2014/10/09 职场文书
班组长如何制订适合本班组的工作计划?
2019/07/10 职场文书
解决Jupyter-notebook不弹出默认浏览器的问题
2021/03/30 Python
MyBatis自定义SQL拦截器示例详解
2021/10/24 Java/Android
Vue全局事件总线你了解吗
2022/02/24 Vue.js
世界十大狙击步枪排行榜
2022/03/20 杂记