深圳市一览网络股份有限
公司
ShenZhen Elanw Network
Co.,Ltd.
地址:南山科技园南区数字技术园
B2 栋 4A
Add: Room A,4th Floor,Block
B2,Digi-Tech Park ,
电话(Tel):0755-86133725
传真(Fax):0755-22632616
网址
(Web):www.YL1001.com
本文主要介绍了一个好用的
PHP 验证码类
实例,有需要的朋友可以参考一下
分享一个好用的
php 验证码类,包括调用示例。
说明:
如果不适用指定的字体,那么就用
imagestring()函数,如果需要遇到指定的字体,就要用到 imagettftext()
函数。字体的位置在
C 盘下 Windows/Fonts.
参考了网上的
php 生成验证码的方法,以及 php 图片验证码和 php 中文验证码的生成方法。用到了 PHP
GD 库的相关知识。
1,生成验证码的类 VerificationCode.
class
.php
代码如下
:
<?php
class
VerificationCode{
private
$charset
="abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";
//随机因子
private
$code
;
//验证码
private
$codelen
=4;
//验证码长度
private
$width
=110;
//宽度
private
$height
=30;
//高度
private
$img
;
//图像资源句柄
private
$font
;
//制定字体
private
$fontSize
=25;
//字体大小
private
$fontColor
;
//字体颜色
public
function
__construct(){
$this
->font="CALIBRIZ.TTF";
}
//生成验证码
private
function
createCode(){
$len
=
strlen
(
$this
->charset)-1;
for
(
$i
= 0;
$i
<
$this
->codelen;
$i
++) {
$this
->code .=
$this
->charset[mt_rand(0,
$len
)];
}
}
//生成背景
private
function
createBg(){
$this
->img=imagecreatetruecolor(
$this
->width,
$this
->height);
$color
= imagecolorallocate(
$this
->img,mt_rand(157,255),mt_rand(157,255),mt_rand(157,255));
imagefilledrectangle(
$this
->img,0,
$this
->height,
$this
->width,0,
$color
);
}
//生成文字
private
function
createFont(){
$x
=
$this
->width/
$this
->codelen;
for
(
$i
= 0;
$i
<
$this
->codelen;
$i
++) {
$this
->fontColor=imagecolorallocate(
$this
->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));