|
Class ImageCode {
Private $ width; // the image width of the verification code.
Private $ height; // The Image height of the verification code
Private $ codeNum; // Number of characters in the verification code
Private $ checkCode; // verification code character
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 createCheckCode (){
For ($ I = 0; $ I <$ this-> codeNum; $ I ){
$ Number = rand (0, 2 );
Switch ($ number ){
Case 0:
$ Rand_number = rand (48, 57); // number
Break;
Case 1:
$ Rand_number = rand (); // uppercase letters
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 does not support Image creation ");
}
}
Function _ destruct (){
Imagedestroy ($ this-> image );
}
}
?> |