php - 使用 Laravel 开发显示图片

查看:95
本文介绍了php - 使用 Laravel 开发显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

首先将php原生代码写在PHP 中, 结果出现

代码如下:

Route::get('png', function () {
    ob_start();
    $im = @imagecreate(200, 50) or die("创建图像资源失败");
    imagecolorallocate($im, 255, 255, 255);
    $text_color = imagecolorallocate($im, 0, 0, 255);
    imagestring($im, 5, 0, 0, "Hello world!", $text_color);
    imagepng($im);
    imagedestroy($im);
    $content = ob_get_clean();
    return response($content, 200, [
        'Content-Type' => 'image/png',
    ]);
});

出现的问题是:

版本: Laravel Framework version 5.1.35 (LTS)

解决方案

$im = @imagecreate(200, 50) or die("创建图像资源失败");
imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 5, 0, 0, "Hello world!", $text_color);
ob_start();
imagepng($im);
$content = ob_get_contents();
imagedestroy($im);
ob_end_clean();
return $response = Response::make($content)->header('Content-Type', 'image/png');

应该是为了规范性 Laravel 对响应头进行了处理,所以你在 console 中会发现你的 Header 头信息被覆盖了,最好都有个 return

这篇关于php - 使用 Laravel 开发显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆