<?php
/**
-
这个是验证码制作经典类
*/
class Vcode{
private $width; //验证码宽度
private $height; //验证码高度
private $codeNum; //验证码字符个数
private $disturbColorNum; //干扰元素数量
private $checkCode; //验证码字符
private $image; //验证码资源
private $str; //生成的字符function __construct($width=80,$height=20,$codeNum=5,$nan=1){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
//设置验证码干扰程度;
switch($nan){
case 1:$this->disturbColorNum=rand(2, 5); break;
case 2:$this->disturbColorNum=rand(5, 7); break;
case 3:$this->disturbColorNum=rand(7, 9); break;
case 4:$this->disturbColorNum=rand(11, 15); break;
case 5:$this->disturbColorNum=rand(15, 20); break;
default:$this->disturbColorNum=35;
}
$this->image=imagecreatetruecolor($this->width, $this->height);}
/**
-
*/
function index(){
$size=rand(3, 5);
$bai=imagecolorallocate($this->image, 255, 255, 255);
imagefill($this->image, 0, 0, $bai);
$this->ganrao($this->disturbColorNum);
$this->str($this->codeNum);for ($i=0;$i<count($this->str);$i++){
$x=floor($this->width/$this->codeNum)*$i+3;
$y=rand(0, $this->height-imagefontheight($size));
imagechar($this->image, $size, $x, $y,$this->str[$i] , imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
}imagepng($this->image);
$this->jiancha();
}
/**
- 生成字符(大写字母 ,小写字母,数字)
- @param int $num 生成字符的个数
*/
function str($num){
$str=array();
for ($i=0;$i<$num;$i++){
$arr=array(chr(rand(48, 57)),chr(rand(97,122)),chr(rand(65,90)));
$str[]=$arr[rand(0, 2)];
}
$this->str=$str;
}
/**
-
干扰点,线函数
*/
function ganrao($num){
//生成线
for($i=0;$i<$num;$i++){
imageline($this->image, rand(0,$this->width), rand(0, $this->height), rand(0,$this->width), rand(0, $this->height),imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));}
//生成点
for($i=0;$i<$num+rand(20,35);$i++){
imagesetpixel($this->image, rand(0,$this->width), rand(0,$this->height), imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
}
}
/**
-
检查 格式有更好的移植性
*/
function jiancha(){if(function_exists("imagegif")){
header("content-type:image/gif");
}else if(function_exists("imageipeg")){
header("content-type:image/jpeg");
}else if(function_exists("imagepng")){
header("content-type:image/png");
}else if(function_exists("imagewbmp")){
header("content-type:image/vnd.wap.wbmp");
}else{
die("在PHP服务器中不支持图像!");
}
}
function session(){
return $this->str;
}
function __destruct(){
imagedestroy($this->image); //释放图像资源
} -
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章