php - 使用 Laravel 开发显示图片
天蓬老师
天蓬老师 2017-04-10 17:52:22
[PHP讨论组]

首先将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)

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
伊谢尔伦
$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

ringa_lee

在输出前使用ob_clean(),

迷茫

这是经过以上几个程序员调整之后的结果. 谢谢大家

Route::get('png', function () {
    ob_clean();
    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',
    ]);
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号