- N +

ThinkPHP6 图片输出-设置响应头

ThinkPHP6 输出图片,每次打开都是一串乱码,看样子是图片源二进制数据,然后查看了下响应头是 text/html,那我明明设置了 header(“Content-type”, “image/jpeg”)。

原因

TP 有个 Response 类,默认所有控制器输出 text/html。

解决

方法一

一种极端的做法是直接 echo 图片 exit。

public function image()
{
    header("Content-type: image/png");
    echo file_get_contents('logo.png');
    exit();
}

方法二

幽雅永不过时。

public function image()
{
    return response()->contentType("image/png")->content(file_get_contents('logo.png'));
}

有好的文章希望我们帮助分享和推广,猛戳这里我要投稿

返回列表
上一篇:
下一篇:

发表评论中国互联网举报中心

快捷回复:

验证码

    评论列表 (暂无评论,共人参与)参与讨论

    还没有评论,来说两句吧...