图片验证码文件,加减法计算方式

/**
 * 图片验证码文件,加减法计算方式
 */
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 = 'arial.ttf'; //字体文件
    private $session = 'code';//session 变量

    private function jiashu() {
        //header('Content-type:image/png');
        header("Content-type:image/JPEG");
        $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='arial.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);//以PNG格式将图像输出到浏览器
        imagedestroy($this->img);//销毁图像
    }
}
$ImageCode = new ImageCode;
$ImageCode->show(130,35,'arial.ttf','code');

转载于:https://www.cnblogs.com/lilyhomexl/p/5632522.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值