Home  >  Article  >  Backend Development  >  A very practical PHP verification code class_PHP tutorial

A very practical PHP verification code class_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:10:17720browse

A more practical PHP graphic verification code generation class, the calling method is also very simple, friends in need can refer to it.

The code is as follows
 代码如下 复制代码

class ImageCode{
private $width;//验证码图片宽度
private $height;//验证码图片高度
private $codeNum;//验证码字符个数
private $checkCode;//验证码字符
private $image;//验证码画布
function __construct($width=60,$height=20,$codeNum=4){
$this->width=$width;
  $this->height=$height;
  $this->codeNum=$codeNum;
  $this->checkCode=$this->createCheckCode();
 }
 
 function getcreateImage(){
  $this->getcreateImage();
  $this->outputText();
  $this->setDisturbColor();
  $this->outputImage();
 }
 function getCheckCode(){
  return $this->checkCode;
 }
 
 private function getCreateImage(){
  $this->image=imagecreatetruecolor($this->width,$this->height);
  $black=imagecolorallocate($this->image,255,255,255,0);
  $border=imagecolorallocate($this->image,255,255,255,255);
  imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
 }

 private function createCheckCode(){
  for($i=0;$i<$this->codeNum;$i){
   $number=rand(0,2);
   switch($number){
    case 0:
     $rand_number=rand(48,57);//数字
     break;
    case 1:
     $rand_number=rand(65,90);//大写字母
     break;
    case 2:
     $rand_number=rand(97,122);
     break;
   }
   $asc=sprintf("%c",$rand_number);
   $asc_number=$asc_number.$asc;
  }
  return $asc_number;
 }

 private function setDisturbColor(){
  for($i=0;$i<=100;$i++){
$color=imagecolorallocate($this->image,255,255,255);
   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  }
 }

 private function outputImage(){
  if(imagetypes()&IMG_GIF){
   header("Content_type:image/gif");
   imagegif($this->image);
  }elseif(imagetypes()&IMG_JGP){
   header("Content_type:image/jpeg");
   imagejpeg($this->image,"",0.5);
  }else{
   die("PHP不支持图像创建");
  }
 }

 function __destruct(){
  imagedestroy($this->image);
 }
}

?>

Copy code

class ImageCode{
private $width;//Verification code image width
private $height;//Verification code image height
private $codeNum;//The number of characters in the verification code
private $checkCode;//Verification code characters
private $image;//Verification code canvas
function __construct($width=60,$height=20,$codeNum=4){
$this->width=$width;
$this->height=$height;
$this->codeNum=$codeNum;
$this->checkCode=$this->createCheckCode();
}

function getcreateImage(){
$this->getcreateImage();
$this->outputText();
$this->setDisturbColor();
$this->outputImage();
}
function getCheckCode(){
Return $this->checkCode;
}

private function getCreateImage(){
$this->image=imagecreatetruecolor($this->width,$this->height);
$black=imagecolorallocate($this->image,255,255,255,0);
$border=imagecolorallocate($this->image,255,255,255,255);
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
}

private function setDisturbColor(){
for($i=0;$i<=100;$i++){
$color=imagecolorallocate($this->image,255,255,255);
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
}
}
private function outputImage(){
if(imagetypes()&IMG_GIF){
header("Content_type:image/gif");
Imagegif($this->image);
}elseif(imagetypes()&IMG_JGP){
header("Content_type:image/jpeg");
Imagejpeg($this->image,"",0.5);
}else{
die("PHP does not support image creation");
}
}
function __destruct(){
imagedestroy($this->image);
}
}
?>
http://www.bkjia.com/PHPjc/629693.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629693.htmlTechArticleA more practical PHP graphic verification code generation class, the calling method is also very simple, friends in need can refer to it one time. The code is as follows Copy the code ?php classImageCode{ private $widt...
Statement:
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