Home > php教程 > php手册 > 简单易看的验证码

简单易看的验证码

WBOY
Release: 2016-06-07 11:40:48
Original
1105 people have browsed it

新手学习thinkphp,感觉自带的验证码不但难看而且很小,自己写了一个GD2的验证码,与大家分享
class Verify{
public $len; //验证码长度
public $width; //宽度
public $height=25; //高度
public $fontfile; //字体库的位置,绝对路径
function __construct($lengs=4,$fontfile="C:/WINDOWS/Fonts/SIMKAI.TTF"){
$this->len=$lengs; //初始化验证码长度
$this->width=$this->len*18; //初始化图片宽度
$this->fontfile=$fontfile; //初始化字体库位置
session_start();
//header("content-type:image/gif");
}
//截取字符串,并将值返回给函数
function text(){
$str="adfghjkqwertyupxvbnmADFHJKQWERTYUPXVBNM12346789";
$str=substr(str_shuffle($str),0,$this->len);
return $str;
}
//制作验证码的图片
function main(){
$str=$this->text();
$x=5;
$y=$this->height-3;
$_SESSION["verify"]=md5(strtolower($str));//将值转换为小写,并传递到session中
$array=str_split($str);
$image=imagecreatetruecolor($this->width,$this->height);
$whirt=imagecolorallocate($image,255,255,255);
imagefilledrectangle($image,0,0,$this->width,$this->height,$whirt);
foreach ($array as $text){
$color=imagecolorallocate($image,mt_rand(50,200),mt_rand(50,200),mt_rand(50,200));
for($i=1;$i imagesetpixel($image,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
$fontfile="C:/WINDOWS/Fonts/SIMKAI.TTF";
imagettftext($image,mt_rand(17,27),mt_rand(-30,30),$x,$y,$color,$fontfile,$text);
$x+=15;

}
imagegif($image);
}
}
//实例化类
$V=new Verify();
$V->main();

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template