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 常用函数库和一些实用小技巧
Jan 01 PHP
提高PHP编程效率 引入缓存机制提升性能
Feb 15 PHP
PHP下对字符串的递增运算代码
Aug 21 PHP
Base64在线编码解码实现代码 演示与下载
Jan 08 PHP
PHP mb_convert_encoding文字编码的转换函数介绍
Nov 10 PHP
慎用preg_replace危险的/e修饰符(一句话后门常用)
Jun 19 PHP
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
Apr 24 PHP
thinkphp实现面包屑导航(当前位置)例子分享
May 10 PHP
php实现的用户查询类实例
Jun 18 PHP
使用JavaScript创建新样式表和新样式规则
Jun 14 PHP
PHP示例演示发送邮件给某个邮箱
Apr 03 PHP
laravel5.5添加echarts实现画图功能的方法
Oct 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
php笔记之:php函数range() round()和list()的使用说明
2013/04/26 PHP
PHP中返回引用类型的方法
2015/04/03 PHP
PHP的Yii框架中移除组件所绑定的行为的方法
2016/03/18 PHP
JS在IE和FireFox之间常用函数的区别小结
2010/03/12 Javascript
Javascript技巧之不要用for in语句对数组进行遍历
2010/10/20 Javascript
js中indexof的用法详细解析
2013/12/24 Javascript
jQuery修改li下的样式以及li下的img的src的值的方法
2014/11/02 Javascript
js实现仿QQ秀换装效果的方法
2015/03/04 Javascript
js控制文本框只输入数字和小数点的方法
2015/03/10 Javascript
详解jQuery Mobile自定义标签
2016/01/06 Javascript
微信小程序 Storage API实例详解
2016/10/02 Javascript
jquery实现瀑布流效果 jquery下拉加载新数据
2016/12/12 Javascript
jQuery查找dom的几种方法效率详解
2017/05/17 jQuery
js实现鼠标移动到图片产生遮罩效果
2017/10/21 Javascript
Less 安装及基本用法
2018/05/05 Javascript
详解Vue CLI 3.0脚手架如何mock数据
2018/11/23 Javascript
vue+elementUI实现表格关键字筛选高亮
2020/10/26 Javascript
详解ES6 Promise的生命周期和创建
2019/08/18 Javascript
vue中重定向redirect:‘/index‘,不显示问题、跳转出错的完美解决
2020/09/28 Javascript
Vue绑定用户接口实现代码示例
2020/11/04 Javascript
python实现读取命令行参数的方法
2015/05/22 Python
python判断数字是否是超级素数幂
2018/09/27 Python
python os.path.isfile 的使用误区详解
2019/11/29 Python
安装python3.7编译器后如何正确安装opnecv的方法详解
2020/06/16 Python
Python 创建守护进程的示例
2020/09/29 Python
使用css3制作动感导航条示例
2014/01/26 HTML / CSS
用HTML5中的Canvas结合公式绘制粒子运动的教程
2015/05/08 HTML / CSS
html5调用app分享功能示例(WebViewJavascriptBridge)
2018/03/21 HTML / CSS
物理力学求职信
2014/02/18 职场文书
小学数学国培感言
2014/03/10 职场文书
《广玉兰》教学反思
2014/04/14 职场文书
关于保护环境的建议书
2014/08/26 职场文书
房屋登记授权委托书范本
2014/10/09 职场文书
校园会短篇的广播稿
2014/10/21 职场文书
python之基数排序的实现
2021/07/26 Python
2022漫威和DC电影上映作品
2022/04/05 欧美动漫