首先將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)
回複內容:
首先將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
。
在輸出前使用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', ]);});