This article mainly introduces the code of the addition and subtraction verification code implemented by php. you can use the addition and subtraction codes of less than 10 to generate images. For more information, see
The code is as follows:
/* Image verification code file, plus or minus calculation method */
Class ImageCode {
Private $ Jiashu = 0; // addition or subtraction
Private $ JianShu = 0; // add number or subtrahend
Private $ YunSuan = ''; // operator
Private $ DeShu = 0; // number
Private $ String = ''; // String style
Private $ Img; // image object
Private $ Width = 100; // image Width
Private $ Height = 50; // Image Height
Private $ Ttf = 'Num. ttf'; // font file
Private $ Session = 'code'; // Session variable
Private function JiaShu (){
Header ('content-type: image/png ');
$ This-> Jiashu = rand (1, 10 );
$ This-> JianShu = rand (1, 10 );
$ This-> YunSuan = $ this-> Jiashu> $ this-> JianShu? '-': '+ ';
$ This-> DeShu = $ this-> Jiashu> $ this-> JianShu? $ This-> Jiashu-$ this-> JianShu: $ this-> Jiashu + $ this-> JianShu;
}
Public function Show ($ W = 100, $ H = 50, $ T = 'Num. ttf', $ Code = 'code '){
$ This-> JiaShu ();
$ This-> String = $ this-> Jiashu. $ this-> YunSuan. $ this-> JianShu. '=? ';
$ This-> Width = $ W;
$ This-> Height = $ H;
$ This-> Ttf = $ T;
$ This-> Session = $ Code;
Session_start ();
$ _ SESSION [$ this-> Session] = $ this-> DeShu;
$ This-> Images ();
}
Private function Images (){
$ This-> Img = imagecreate ($ this-> Width, $ this-> Height );
$ Background_color = imagecolorallocate ($ this-> image, 255,255,255 );
Imagecolortransparent ($ this-> Img, $ background_color );
Imagettftext ($ this-> Img, 14, 0, 1, 20, imagecolorallocate ($ this-> Img, 0, 0, 0), $ this-> Ttf, $ this-> String );
$ This-> EchoImages ();
}
Private function EchoImages (){
Imagepng ($ this-> Img );
Imagedestroy ($ this-> Img );
}
}
$ ImageCode = new ImageCode;
$ ImageCode-> Show (130, 35, 'Num. ttf', 'code ');