加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现加减法验证码代码

发布时间:2020-12-12 20:03:47 所属栏目:PHP教程 来源:网络整理
导读:代码如下: /*图片验证码文件,加减计算方式*/ class ImageCode{private $Jiashu = 0; //加数或者减数 private $JianShu = 0; //被加数或者被减数 private $YunSuan = ''; //运算符 private $DeShu = 0; //得数 private $String = ''; //字符串样式 private $

代码如下:
/*图片验证码文件,加减计算方式*/

class ImageCode{private $Jiashu = 0; //加数或者减数
private $JianShu = 0; //被加数或者被减数
private $YunSuan = ''; //运算符
private $DeShu = 0; //得数
private $String = ''; //字符串样式
private $Img; //图片对象
private $Width = 100; //图片宽度
private $Height = 50; //图片高度
private $Ttf = 'Num.ttf';//字体文件
private $Session = 'code'; //Session变量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);
imagecolortransparent($this -> Img,$background_color);
imagettftext($this -> Img,14,1,20,imagecolorallocate ($this -> Img,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');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读