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中array_merge和array相加的区别分析
Jun 17 PHP
php定义数组和使用示例(php数组的定义方法)
Mar 29 PHP
php生成短网址示例
May 05 PHP
php计算两个日期时间差(返回年、月、日)
Jun 19 PHP
PHP中一些可以替代正则表达式函数的字符串操作函数
Nov 17 PHP
Laravel中注册Facades的步骤详解
Mar 16 PHP
Symfony2学习笔记之模板用法详解
Mar 17 PHP
PHP读取大文件的几种方法介绍
Oct 27 PHP
php事务回滚简单实现方法示例
Mar 28 PHP
php图片裁剪函数
Oct 31 PHP
PHP hex2bin()函数用法讲解
Feb 25 PHP
php进程(线程)通信基础之System V共享内存简单实例分析
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
冰滴咖啡制作步骤
2021/03/03 冲泡冲煮
PHP的历史和优缺点
2006/10/09 PHP
PHP 中dirname(_file_)讲解
2007/03/18 PHP
PHP iconv 解决utf-8和gb2312编码转换问题
2010/04/12 PHP
PHP运行时强制显示出错信息的代码
2011/04/20 PHP
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
2014/05/15 PHP
PHPStrom中实用的功能和快捷键大全
2015/09/23 PHP
php插件Xajax使用方法详解
2017/08/31 PHP
Add a Picture to a Microsoft Word Document
2007/06/15 Javascript
CSS鼠标响应事件经过、移动、点击示例介绍
2013/09/04 Javascript
js 左右悬浮对联广告特效代码
2014/12/12 Javascript
JavaScript实现将数组中所有元素连接成一个字符串的方法
2015/04/06 Javascript
javascript数组去重的六种方法汇总
2015/08/16 Javascript
JavaScript实现广告弹窗效果
2016/08/09 Javascript
Node.js中路径处理模块path详解
2016/11/14 Javascript
ztree实现权限横向显示功能
2017/05/20 Javascript
解决Vue不能检测数组或对象变动的问题
2018/02/24 Javascript
微信小程序顶部导航栏滑动tab效果
2019/01/28 Javascript
通过Nodejs搭建网站简单实现注册登录流程
2019/06/14 NodeJs
Vue $emit()不能触发父组件方法的原因及解决
2020/07/28 Javascript
[26:21]浴火之凤-TI4世界冠军Newbee战队纪录片
2014/08/07 DOTA
Python多层嵌套list的递归处理方法(推荐)
2016/06/08 Python
Python使用MD5加密算法对字符串进行加密操作示例
2018/03/30 Python
利用pandas将非数值数据转换成数值的方式
2019/12/18 Python
Python常用模块sys,os,time,random功能与用法实例分析
2020/01/07 Python
python判断字符串以什么结尾的实例方法
2020/09/18 Python
Python自动化测试基础必备知识点总结
2021/02/07 Python
使用css创建三角形 使用CSS3创建3d四面体原理及代码(html5实践)
2013/01/06 HTML / CSS
全球烹饪课程的领先预订平台:Cookly
2020/01/28 全球购物
EJB发布WEB服务一般步骤
2012/10/31 面试题
名人演讲稿范文
2013/12/28 职场文书
英语专业职业生涯规划范文
2014/03/05 职场文书
小学生期末评语
2014/04/21 职场文书
教师年度考核个人总结
2015/02/12 职场文书
2016中秋节月饼促销广告语
2016/01/28 职场文书
励志语录:你若不勇敢,谁替你坚强
2019/11/08 职场文书