使用 Laravel 开发显示图片
首先将php原生代码写在PHP 中, 结果出现
代码如下:
<code>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', ]); });</code>
出现的问题是:
版本: Laravel Framework version 5.1.35 (LTS)
回复内容:
首先将php原生代码写在PHP 中, 结果出现
代码如下:
<code>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', ]); });</code>
出现的问题是:
版本: Laravel Framework version 5.1.35 (LTS)
<code>$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');</code>
应该是为了规范性 Laravel
对响应头进行了处理,所以你在 console
中会发现你的 Header
头信息被覆盖了,最好都有个 return
。
在输出前使用ob_clean(),
这是经过以上几个程序员调整之后的结果. 谢谢大家
<code>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', ]); });</code>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu
