验证码(规整)

<?php
class CheckCode{
 private $width;
 private $height;
 private $im;
 private $text;
 
 function __construct($width,$height){
  $this->width=$width;
  $this->height=$height;
  }
  
 function CreateCode(){
  $this->ImageIni();//初始化图像
  $this->CreateText();//生成验证码
  $this->setDisturbColor();//设置干扰点
  $this->outputImage();//向浏览器输出图片
  
  }
  
  
  //定义初始图像函数
 private function ImageIni(){
  $this->im = imagecreatetruecolor($this->width,$this->height);
  $bgcolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
  $bordercolor = imagecolorallocate($this->im,0,0,0);
  imagefill($this->im,$this->width-1,$this->height-1,$bgcolor);
  imagerectangle($this->im,1,1,$this->width-1,$this->height-1,$bordercolor);  
  }
 
 //定义产生随机字符函数
 private function CreateText(){ 
   $tcolor = imagecolorallocate($this->im,125,0,255); 
   for($i = 0;$i < 4; $i++){
   $num_case = rand(0,2);//产生随机数0-2,根据数值的不同决定产生的是数字|小写|大写
   switch($num_case){
    case 0:$num = rand(48,57);break;//数字
    case 1:$num = rand(65,90);break;//大写
    default:$num = rand(97,122);//小写
        }
 $this->text[$i] = sprintf("%c",$num);//将随机产生的ASCII码转换为相应的字符
 imagettftext($this->im,$this->height-5,rand(0,45),(($this->width-10)/4)*$i+5,$this->height-3,$tcolor,"STLITI.TTF",$this->text[$i]);//显示字符
   }  
  }
 
 //创建放置干扰点函数
 private function setDisturbColor(){
  $green = imagecolorallocate($this->im,0,255,0);
  for($i=0;$i<100;$i++){
  imagesetpixel($this->im,rand(1,$this->width-1),rand(1,$this->height-1),$green);
   }
  }
 
 //创建图像输出函数
 private function outputImage(){
  if (imagetypes() & IMG_GIF){
  header("Content-type:image/gif");//设置输出类型
  imagegif($this->im);//输出图像
    }
  elseif(imagetypes() & IMG_PNG){
   header("Content-type:image/png");//设置输出类型
   imagepng($this->im);//输出图像
    }
  elseif(imagetypes() & IMG_JPG){
   header("Content-type:image/jpeg");//设置输出类型
   imagejpeg($this->im);//输出图像
    }
  elseif(imagetypes() & IMG_WBMP){
   header("Content-type:image/wbmp");//设置输出类型
   imagewbmp($this->im);//输出图像
    }
  else {
   die("php不支持您的图像创建");
    }  
  }

function getMessage(){
 return implode($this->text);
 }
  
  
  
  }

//析构函数销毁图像,释放内存
function __destruct(){
 imagedestroy($this->im);
 
 }
  
  
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值