Developing display images using Laravel

Source: Internet
Author: User
Keywords php laravel5.1

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',    ]);});
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.