PHP封装验证码类

来源:互联网 发布:劲舞团做图软件 编辑:程序博客网 时间:2024/06/03 18:57
<?php
class Vercode{
protected $codeNum;
protected $width;
protected $height;
protected $img;
function __construct($width="80",$height="40",$codeNum="4"){
 $this->width=$width;
 $this->height=$height;
 $this->codeNum=$codeNum;


 }
 protected function createImage(){
 $this->img=imagecreatetruecolor($this->width,$this->height);
 $color=imagecolorallocate($this->img,rand(100,155),rand(100,155),rand(100,155));
 @imagefill($this->img,0,0,$color);
 }
 protected function random(){
  if($this->codeNum<=0){
     return false;
  }
  $str="0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    $str=substr(str_shuffle($str),0,$this->codeNum);
    return $str;
 }
 protected function outImg(){
  for($i=0;$i<40;$i++){
    $linecolor=imagecolorallocate($this->img,rand(100,250),rand(100,250),rand(100,250));
    imageline($this->img,rand(0,$this->width-10),rand(0,$this->height-10),rand(0,$this->width),rand($this->width-10,$this->height-10),$linecolor);
  }
 $strcolor=imagecolorallocate($this->img,250,250,250);
 imagestring($this->img,5,rand(10,$this->height-10),rand(10,$this->height-10),$this->random(),$strcolor);
 header("Content-type:image/png");
 imagepng($this->img);
 }
 function Imgcode(){
$this->createImage();
$this->outImg();
echo $this->codeNum;
 }
 function __destruct(){
  imagedestroy($this->img);
 }
}
$vercode=new Vercode(100,50,6);
$vercode->Imgcode();
?>
0 0
原创粉丝点击