First the PHP native code is written in PHP, the results appear
The code is as follows:
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', ]);});
The problems that arise are:
Version: Laravel Framework version 5.1.35 (LTS)
Reply content:
First the PHP native code is written in PHP, the results appear
The code is as follows:
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', ]);});
The problems that arise are:
Version: 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');
The response header should be handled in a normative way Laravel
, so you console
will find that your Header
header information is overwritten, preferably with one return
.
Use Ob_clean () before the output,
This is the result of the adjustment of the above several programmers. Thank you
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', ]);});