/*
问题:验证码可以正常显示,说明验证码已经生成(分别是$code和$gbkcode),但是通过属性值获取不到验证码,为空。 return $this->code; 为空?
求解.............
*/
<?php
class VerificationCode {
private $width; //验证码宽度
private $height;//验证码高度
private $codeNum;//验证码位数
private $image; //图像资源
private $code; //验证码字符串
private $gbkcode;//中文验证码字符串
private $flag;//使用中文还是英文验证码标志位,1表示英文,0表示中文,
function __construct($width=80,$height=20,$codeNum=4,$flag=1){ //初始化对象属性
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->flag=$flag;
}
function displayImage(){ //创建显示验证码图片的方法作为共有方法供外部调用
//创建图像背景
$this->creatImage();
//设置干扰元素(点和线)
$this->setDisturb();
//创建验证码
$this->creatCode();
//输出图像
$this->outPutImage();
}
function getCheckCode(){ //获取验证码的方法,可以外部调用,问题:读不出来验证码的值
if ($this->flag==1)
return $this->code;
if ($this->flag==0)
return $this->gbkcode;
}
function creatImage(){
//创建图像资源
$this->image=imagecreatetruecolor($this->width, $this->height);
//创建随机背景色
$backColor=imagecolorallocate($this->image, rand(225, 255), rand(225,255), rand(225, 255));
//为背景添充颜色
imagefill($this->image, 0, 0, $backColor);
//设置边框颜色
$border=imagecolorallocate($this->image, 0, 0, 0);
//画出矩形边框
imagerectangle($this->image, 0, 0, $this->width-1, $this->height-1, $border);
}
private function setDisturb(){
for($i=0; $i<100; $i++){ //设置100个随机干扰点阵
$color=imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)); //生成随机颜色
imagesetpixel($this->image, rand(1, $this->width-2), rand(1, $this->height-2), $color); //使用随机颜色生成随机点
}
for($i=0; $i<10; $i++){
$color=imagecolorallocate($this->image, rand(200, 255), rand(200, 255), rand(200, 255));//生成随机颜色
imageline ($this->image, rand(-10, $this->width), rand(-10, $this->height), rand(30, 300), rand(20, 200), $color);//使用随机颜色生成随机点
}
}
private function creatCode(){
if($this->flag==1){
for($i=0; $i < $this->codeNum; $i++){ //循环生成验证码随机字符串
$rand=dechex(rand(1, 15));
$code.=$rand;
}
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128)); //随机字体颜色
imagestring($this->image,5,10,3,$code,$fontcolor); //向已经有干扰元素的背景图片写入验证码字符串
//return $this->$code;
}elseif ($this->flag==0){
$str=array("为","答","谢","广","可","家","积","品","一","起","发","放");
//iconv("UTF-8", "GB2312", $str);
$gbkcode='';
for($i=0; $i < $this->codeNum; $i++){
$char=$str[rand(0, count($str)-1)];
$gbkcode.=$char;
}
$fontcolor=imagecolorallocate($this->image, rand(0, 128), rand(0, 128), rand(0, 128)); //随机字体颜色
imagettftext($this->image,12,0,10,15,$fontcolor,"STFANGSO.TTF",$gbkcode); //向已经有干扰元素的背景图片写入中文验证码字符串
//return $this->$gbkcode;
}
else {
echo "参数有误!";
}
}
private function outPutImage(){ //判断当前php支持的图片版本并输出图片资源
if(imagetypes() & IMG_GIF){
header("Content-Type:image/gif");
imagepng($this->image);
}else if(imagetypes() & IMG_JPG){
header("Content-Type:image/jpeg");
imagepng($this->image);
}else if(imagetypes() & IMG_PNG){
header("Content-Type:image/png");
imagepng($this->image);
}else if(imagetypes() & IMG_WBMP){
header("Content-Type:image/vnd.wap.wbmp");
imagepng($this->image);
}else{
die("PHP不支持图像创建");
}
}
function __destruct(){ //销毁图像资源
imagedestroy($this->image);
}
}