使用 Laravel 開發顯示圖片

來源:互聯網
上載者:User
關鍵字 php laravel5.1

首先將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',    ]);});
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.