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

 更新时间:2016年06月15日 10:23:09   投稿:lijiao  
这篇文章主要为大家详细介绍了PHP实现适用于自定义的验证码类,使用对象编写的验证码类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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中改变图片的尺寸大小的代码

    PHP中改变图片的尺寸大小的代码

    改变图片的尺寸是一个很常见的功能需求,下面开始研究下关于PHP改变图片尺寸的方法。
    2011-07-07
  • php中目录操作opendir()、readdir()及scandir()用法示例

    php中目录操作opendir()、readdir()及scandir()用法示例

    这篇文章主要介绍了php中目录操作opendir()、readdir()及scandir()用法,结合具体实例形式分析了PHP使用opendir()、readdir()及scandir()读取目录的相关操作技巧,需要的朋友可以参考下
    2019-06-06
  • PHP截取指定图片大小的方法

    PHP截取指定图片大小的方法

    这篇文章主要介绍了PHP截取指定图片大小的方法,可实现对指定图片的缩放与截取功能,是非常实用的技巧,需要的朋友可以参考下
    2014-12-12
  • php常用字符串处理函数实例分析

    php常用字符串处理函数实例分析

    这篇文章主要介绍了php常用字符串处理函数,以实例形式分析了chop()、get_html_translation_table()、htmlentities()及htmlspecialchars()等函数的具体用法,对于PHP的学习有着一定的学习与借鉴价值,需要的朋友可以参考下
    2014-11-11
  • PHP中key和current,next的联合运用实例分析

    PHP中key和current,next的联合运用实例分析

    这篇文章主要介绍了PHP中key和current,next的联合运用,结合实例形式分析了key和current,next操作数组元素的相关技巧,需要的朋友可以参考下
    2016-03-03
  • php实现有序数组旋转后寻找最小值方法

    php实现有序数组旋转后寻找最小值方法

    在本篇文章中我们给大家详细分享了php实现有序数组旋转后寻找最小值方法,有需要的朋友们可以学习下。
    2018-09-09
  • php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例

    php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例

    这篇文章主要介绍了php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率,结合完整实例形式对比分析了php分别使用mysqli和pdo扩展连接mysql数据库的执行时间,需要的朋友可以参考下
    2019-05-05
  • 关于crontab的使用详解

    关于crontab的使用详解

    本篇文章是对crontab的使用进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHP连接数据库实现页面增删改查效果

    PHP连接数据库实现页面增删改查效果

    这篇文章主要介绍了如何利用PHP实现连接SQL数据库,从而对页面进行增删改查功能,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-03-03
  • 浅谈PHP无限极分类原理

    浅谈PHP无限极分类原理

    这篇文章主要介绍了PHP无限极分类原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论