PHP实现适用于自定义的验证码类

所属分类: 网络编程 / PHP编程 阅读数: 1301
收藏 0 赞 0 分享

本文实例为大家分享了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);
 }
}

效果图:

精彩专题分享:ASP.NET验证码大全 PHP验证码大全 java验证码大全

以上就是使用对象编写的验证码类的全部内容,希望对大家学习PHP程序设计有所帮助。

更多精彩内容其他人还在看

php addslashes和mysql_real_escape_string

本文介绍的是用 mysql_real_escape_string对用户提交数据进行整理处理和通过addslashes以及mysql_escape_string这3个类似的功能函数的区别。经过转义的数据可以直接插入到数据库中。
收藏 0 赞 0 分享

php cout&lt;&lt;的一点看法

cout<<运算符后面如果是常整数,那么最大可以输出4294967295,即2^32-1,不允许出现负数,也就是说,cout<<运算符对于常整数只重载了unsigned int型;对于有小数的情况,则保留6位有效数字.
收藏 0 赞 0 分享

PHP 变量的定义方法

在网页中使用变量的几点注意事项小结。
收藏 0 赞 0 分享

php学习之 认清变量的作用范围

变量的存在有着它的生命周期,我们可以让它存在于一个小的函数内部,也可让它存在于整个程序当中。对于一般情况下声明的变量,我们称之为局部变量,只能在当前程序段中存在,而使用$globals声明出来的变量则是在当前页面整个程序当中都会有效。
收藏 0 赞 0 分享

php 静态变量与自定义常量的使用方法

php 静态变量与自定义常量的使用方法
收藏 0 赞 0 分享

认识并使用PHP超级全局变量

超级全局变量也叫做预定义变量,是PHP系统中自带的变量,它可让你的程序设计更加的方便快捷。
收藏 0 赞 0 分享

通过具体程序来理解PHP里面的抽象类

面向对象程序通过类的分层结构构建起来. 在单重继承语言如PHP中, 类的继承是树状的. 一个根类有一个或更多的子类,再从每个子类继承出一个或更多下一级子类.
收藏 0 赞 0 分享

php读取xml实例代码

php读取xml实例代码,需要的朋友可以参考下。
收藏 0 赞 0 分享

php 正确解码javascript中通过escape编码后的字符

js的escape如何在PHP中来解呢?下面的这个函数可以正确的解析,网上有不少unescape的函数,但好用的不多.
收藏 0 赞 0 分享

在PHP中养成7个面向对象的好习惯

在 PHP 编程早期,PHP 代码在本质上是限于面向过程的。过程代码 的特征在于使用过程构建应用程序块。过程通过允许过程之间的调用提供某种程度的重用。
收藏 0 赞 0 分享
查看更多