tp图片加文字并输出

分类首页日期4个月前访问185评论0

代码示例:

注意使用fastadmin框架输出图片的时候一定要加上exit();,不然显示的就是图片编码

public function certificateimg($id='')
    {
        $data = [];
        if($id){
            $data = model('Student')->find($id);
            if(!$data){
                $filename = ROOT_PATH.'/public/assets/img/circle.png';
                header('Content-Type: image/png');
                readfile($filename);
                exit();
            }
        }
        $y = 350;
        $textLength = mb_strlen($data['name']);
        if($textLength==2){
            $y = 370;
        }
        
        $dst_path = ROOT_PATH.'/public/assets/img/certificate.jpg';
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        //打上文字
        $font = ROOT_PATH.'/public/assets/fonts/SourceHanSansK-Regular.ttf';//字体路径
        $black = imagecolorallocate($dst, 52,52,88);//字体颜色
        imagefttext($dst, 40,0,$y,656, $black, $font, $data['name']);
        //输出图片
        // list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        header('Content-Type: image/png');
        $show_pic = imagejpeg($dst);
        // imagepng($dst);
        imagedestroy($dst);
        exit();
    }