php 图片加文字点击按钮下载

php 专栏收录该内容
4 篇文章 0 订阅

接上一篇,实现点击按钮下载

image.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>image</title>
</head>
<body>
<div>
<img src="/index/image/image" style="width: 100%" />
<a href="/index/image/image/download/1">//这里带一个download=1的值
<input type="button" value="下载">//多加一个下载按钮
</a>
</div>
</body>
</html>

Image.php

<?php

namespace app\index\controller;
use think\Request;
use think\Controller;

class Image extends Controller{
    public $image;
    public function image($download=0){//默认不下载就是download=0
        $obj = new \app\index\model\ImageClass('static/php.jpg');  //public里的路径图片
        $obj->fontMark('名字:',30,250);
        $obj->fontMark('蟹黄包秘方',80,250);
        if(!$download){    //这里做一个if,选择性跳转
            $obj->show();
        }else{
            $obj->download();
        }
    }
    public function star(){    //这里用来打开html页面;
        return $this->fetch('html/image');
    }
}

ImageClass.php

<?php

namespace app\index\model;
use think\Db;
use think\Controller;

class ImageClass extends Controller{
    private $image;
    private $info;
    private $font;
    private $color;
    function __construct($src){
        $info = getimagesize($src);
        $type = image_type_to_extension($info[2],false);
        $this -> info =$info;
        $this->info['type'] = $type;      //这里$type的值是jpeg;
        $fun = "imagecreatefrom" .$type;
        $this -> image = $fun($src);
        $this->font = env('root_path').'/public/static/fonts/Deng.ttf';//文字路径;
        $this->color = imagecolorallocate($this->image,0,0,0);//字体颜色;
    }
    public function show(){
        header('content-type:' . $this -> info['mime']);   //[mime] => image/jpeg;
        $fun='image' . $this->info['type'];
        $fun($this->image);
    }
    public function  fontMark($text, $x,$y,$fontsize=15){
        imagettftext($this->image,$fontsize,0,$x,$y, $this->color, $this->font,$text);
        //这里用imagettftext()加中文文字,用imageString()是加不了中文的;
        //font一定要是中文库;
        //imagettftext()顺序错了会报错;
    }
    public function download(){    //这里多加一个download();
        header('Content-Disposition:attachment;filename=user.jpg'); //filename是下载的名字
        $fun='image' . $this->info['type'];
        $fun($this->image);
    }
}
  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值