PHP dynamically generates random captcha class code

  • 2020-03-31 20:31:33
  • OfStack

Below is the effect diagram, this effect diagram is not open interference code effect diagram
< img border = 0 SRC = "http://files.jb51.net/upload/2010-4/20100409165642445.jpg" >
Here is the class code

 
<?php 
/************************************************ 
//FILE:ImageCode 
//DONE: generate dynamic captcha classes
//DATE"2010-3-31 
//5D happy blog
************************************************************************/ 
class ImageCode{ 
private $width; //Captcha image width
private $height; //Captcha image height
private $codeNum; //Number of verification code characters
private $checkCode; //Verification code character
private $image; //Captcha canvas
/************************************************************************ 
//Function: constructor
//Done: member property initialization
// 5D happy blog
************************************************************************/ 
function __construct($width=60,$height=20,$codeNum=4) 
{ 
$this->width = $width; 
$this->height = $height; 
$this->codeNum = $codeNum; 
$this->checkCode = $this->createCheckCode(); 
} 
function showImage() 
{ 
$this->getcreateImage(); 
$this->outputText(); 
$this->setDisturbColor(); 
$this->outputImage(); 
} 
function getCheckCode() 
{ 
return $this->chekCode; 
} 
private function getCreateImage() 
{ 
$this->image = imagecreatetruecolor($this->width,$this->height); 
$back = imagecolorallocate($this->image,255,255,255); 
$border = imagecolorallocate($this->image,255,255,255); 
imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border); 
//Use pure white to fill the rectangle box
 
//imagerectangle($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;//digital
case 1: $rand_number = rand(65,90);break;//The capital letters
case 2: $rand_number = rand(97,122);break;//Lowercase letters
} 
$asc = sprintf("%c",$rand_number); 
$asc_number = $asc_number.$asc; 
} 
return $asc_number; 
} 
private function setDisturbColor()//Interference Settings
{ 
for($i=0;$i<=100;$i++) 
{ 
//$color = imagecolorallocate($this->image,rand(0,255),rand(0,255),rand(0,255)); 
$color = imagecolorallocate($this->image,255,255,255); 
imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 
} 
//$color = imagecolorallocate($this->image,0,0,0); 
//imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color); 
} 
private function outputText() 
{ 
//Random color, random placement, random string output to the image
for($i=0;$i<=$this->codeNum;$i++) 
{ 
$bg_color = imagecolorallocate($this->image,rand(0,255),rand(0,128),rand(0,255)); 
$x = floor($this->width/$this->codeNum)*$i+3; 
$y = rand(0,$this->height-15); 
imagechar($this->image,5,$x,$y,$this->checkCode[$i],$bg_color); 
} 
} 
private function outputImage() 
{ 
if(imagetypes()&IMG_GIF) 
{ 
header("Content_type:image/gif"); 
imagegif($this->image); 
} 
elseif(imagetypes()&IMG_JPG) 
{ 
header("Content-type:image/jpeg"); 
imagejpeg($this->image,"",0.5); 
} 
elseif(imagetypes()&IMG_PNG) 
{ 
header("Content-type:image/png"); 
imagejpeg($this->image); 
} 
elseif(imagetypes()&IMG_WBMP) 
{ 
header("Content-type:image/vnd.wap.wbmp"); 
imagejpeg($this->image); 
} 
else 
{ 
die("PHP Image creation is not supported "); 
} 
} 
function __destruct() 
{ 
imagedestroy($this->image); 
} 
} 
 
 
?> 

Related articles: