4-5验证码类封装

1.Captcha.php

  1 <?php
  2 /**
  3  * Captcha.php
  4
  5  * description 验证码类
  6  */
  7
  8 namespace Imooc\Lib;
  9
 10 require_once ‘GDBasic.php‘;
 11
 12 class Captcha extends GDBasic
 13 {
 14     //图像宽度
 15     protected $_width = 60;
 16     //图像高度
 17     protected $_height = 25;
 18
 19     //随机串
 20     protected $_code = ‘ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjklmnpqrstuvwxyz‘;
 21
 22     //字体文件
 23     protected $_font_file = ‘./font/comicz.ttf‘;
 24
 25     //图像
 26     protected $_im;
 27     //验证码
 28     protected $_captcha;
 29
 30     public function __construct($width = null, $height = null)
 31     {
 32         self::check();
 33         $this->create($width, $height);
 34     }
 35
 36     /**
 37      * 创建图像
 38      * @param $width
 39      * @param $height
 40      */
 41     public function create($width, $height)
 42     {
 43         $this->_width = is_numeric($width) ? $width : $this->_width;
 44         $this->_height = is_numeric($height) ? $height : $this->_height;
 45         //创建图像
 46         $im = imagecreatetruecolor($this->_width, $this->_height);
 47         $back = imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
 48         //填充底色
 49         imagefill($im, 0, 0, $back);
 50         $this->_im = $im;
 51     }
 52
 53     /**
 54      * 混乱验证码
 55      */
 56     public function moll()
 57     {
 58         $back = imagecolorallocate($this->_im, 0, 0, 0);
 59         //在图像中随机生成50个点
 60         for($i = 0; $i < 50; $i++)
 61         {
 62             imagesetpixel($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 63         }
 64
 65         imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 66
 67         imageline($this->_im, mt_rand(0, $this->_width), mt_rand(0, $this->_height), mt_rand(0, $this->_width), mt_rand(0, $this->_height), $back);
 68     }
 69
 70
 71     /**
 72      * 生成验证码随机串
 73      * @param int $length 验证码的个数
 74      * @param int $fontSize 字符串的字体大小
 75      * @return Captcha
 76      */
 77     public function string($length = 4, $fontSize = 15)
 78     {
 79         $this->moll();
 80         $code = $this->_code;
 81         $captcha = ‘‘;
 82         for($i = 0; $i < $length; $i++)
 83         {
 84             $string = $code[mt_rand(0, strlen($code) - 1)];
 85             $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
 86             imagettftext($this->_im, $fontSize, mt_rand(-10, 10), mt_rand(3, 6) + $i * (($this->_width - 10) / $length), ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
 87             $captcha .= $string;
 88         }
 89
 90         $this->_captcha = $captcha;
 91         return $this;
 92     }
 93
 94
 95     /**
 96      * 验证码存入session
 97      */
 98     public function setSession()
 99     {
100         if(!isset($_SESSION))
101         {
102             session_start();
103         }
104         $_SESSION[‘captcha_code‘] = $this->_captcha;
105     }
106
107     /**
108      * 逻辑运算符验证码
109      * @param int $fontSize 字体大小
110      * @return $this
111      */
112     public function logic($fontSize = 12)
113     {
114         $this->moll();
115         $codeArray = array(1 => 1, 2, 3, 4, 5, 6, 7, 8, 9);
116         $operatorArray = array(‘+‘ => ‘+‘, ‘-‘ => ‘-‘, ‘x‘ => ‘*‘);
117         list($first, $second) = array_rand($codeArray, 2);
118         $operator = array_rand($operatorArray);
119         $captcha = 0;
120         $string = ‘‘;
121         switch($operator)
122         {
123             case ‘+‘:
124                 $captcha = $first + $second;
125                 break;
126             case ‘-‘:
127                 //当第一个数小于第二个数
128                 if($first < $second)
129                 {
130                     list($first, $second) = array($second, $first);
131                 }
132                 $captcha = $first - $second;
133                 break;
134
135             case ‘x‘:
136                 $captcha = $first * $second;
137                 break;
138         }
139         //设置验证码类变量
140         $this->_captcha = $captcha;
141         //要输出到图像中的字符串
142         $string = sprintf(‘%s%s%s=?‘, $first, $operator, $second);
143
144         $strColor = imagecolorallocate($this->_im, mt_rand(100, 150), mt_rand(100, 150), mt_rand(100, 150));
145         imagettftext($this->_im, $fontSize, 0, 5, ($this->_height / 3) * 2, $strColor, $this->_font_file, $string);
146
147         return $this;
148     }
149
150
151     /**
152      * 输出验证码
153      */
154     public function show()
155     {
156         //生成session
157         $this->setSession();
158         header(‘Content-Type:image/jpeg‘);
159         imagejpeg($this->_im);
160         imagedestroy($this->_im);
161     }
162 }

调用

1 require_once ‘./lib/Captcha.php‘;
2
3 $captcha = new \Imooc\Lib\Captcha(80,30);
4
5 $captcha->string(ii6,14)->show();
6 $captcha->logic(12)->show();

原文地址:https://www.cnblogs.com/kay-learning/p/8973230.html

时间: 2024-08-03 02:06:03

4-5验证码类封装的相关文章

PHP生成缩略图、验证码类封装

1 <?php 2 /*如何知道图片的类型和大 3 * 利用getimagesize(),获得以下属性 4 Array 5 ( 6 [0] => 533 //宽 7 [1] => 300 //高 8 [2] => 2 //图片类型 jpg 9 [3] => width="533" height="300" 10 [bits] => 8 11 [channels] => 3 12 [mime] => image/jpeg

2、载入验证码类及$_SESSION处理

1.载入验证码类,并验证 (1)下载定义好的code验证码类,放置到resources目录下 (2) 添加路由 Route::get('/admin/code','Admin\[email protected]'); (3)添加方法 LoginController.php public function code() { $code = new \Code(); $code->make(); } 注意: 不要忘记引入Code类,不加'\',会引入App\Http\Controllers\Admi

PHP验证码类

通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度.高度及验证码字母个数,就可以成功创建一个验证码类的对象.该类的声明代码如下所示: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

THINKPHP源码学习--------验证码类

TP3.2验证码类的理解 今天在学习中用到了THINKPHP验证码,为了了解究竟,就开始阅读TP验证码的源码. 源码位置:./ThinkPHP/Library/Think/Verify.class.php 首先分段来阅读源码 1.namespace Think; Class Verify表示Thinkphp命名空间下的Verify类 2. protected $config = array( 'seKey'    => 'ThinkPHP.CN', // 验证码加密密钥 'codeSet'  =

实现图片验证码类 PHP

封装一个图片验证码类 <?php class Captcha{ private $img; private $imgX; private $imgY; private $codeNum; private $code; private $str="abcdefghjklmnpqrstuvwxyz123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; public function __construct($imgX=80,$imgY=40,$codeNum=4){

一个经典的PHP验证码类分享

我们通过PHP的GD库图像处理内容,设计一个验证码类Vcode.将该类声明在文件vcode.class.php中,并通过面向对象的特性将一些实现 的细节封装在该类中.只要在创建对象时,为构造方法提供三个参数,包括创建验证码图片的宽度.高度及验证码字母个数,就可以成功创建一个验证码类的对象. 该类的声明代码如下所示: <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $co

PHP面向对象简易验证码类

PHP简易验证码类 1 <?php 2 3 class authCode 4 { 5 private static $instance = null; #实例对象 6 private $width = 120; #图片宽度 7 private $height = 40; #图片高度 8 private $font = 'font/elephant.ttf'; #字体文件路径 9 private $fontSize = 14; #字体大小 10 private $strLen = 6; #字符个数

ThinkPHP 3.2.3 加减乘法验证码类

ThinkPHP 3.2.3 自带的验证码类位于 /ThinkPHP/Library/Think/Verify.class.php,字体文件位于 /ThinkPHP/Library/Think/Verify/ 可以在 Verify.class.php 文件内进行修改,也可以单独写一个类继承自带的验证码类.如果单独写一个继承的类,可以重用父类的属性和方法,但是要注意的是父类中有一些属性和方法是私有(private)的,可以修改这些私有的属性和方法为保护(protected)的,如果不希望修改框架自

建立一个漂亮的PHP验证码类文件及调用方式

//验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子 private $code;//验证码 private $codelen = 4;//验证码长度 private $width = 130;//宽度 private $height = 50;//高度 private $img;//图形资源句柄 private $font;//指定的字体