Home  >  Article  >  Backend Development  >  PHP implements addition and subtraction verification code code_PHP tutorial

PHP implements addition and subtraction verification code code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:56716browse

Copy code The code is as follows:

/*Image verification code file, addition and subtraction calculation method */

class ImageCode{

private $Jiashu = 0; //Addend or subtrahend
private $JianShu = 0; //Addend or minuend
private $YunSuan = ''; //Operator
private $DeShu = 0; //Result number
private $String = ''; //String style
private $Img; $Width = 100; //Picture width
private $Height = 50; //Picture 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 -> Img, 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');

http://www.bkjia.com/PHPjc/825253.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825253.htmlTechArticleCopy the code as follows: ?php /*Image verification code file, addition and subtraction calculation method*/ class ImageCode{ private $Jiashu = 0; //addend or subtractor private $JianShu = 0; //addend or subtractor...
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