微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建无法在laravel中工作的形状imagecreatetruecolor

如何解决创建无法在laravel中工作的形状imagecreatetruecolor

我正在尝试在PHP laravel中创建一些形状,例如圆形,矩形。我的程序在正常的PHP文件中工作正常。但是,每当我尝试在laravel中添加代码时,它都会返回一个特殊字符。

geometric.blade.PHP

<?PHP
header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200,200);

// Allocate colors
$pink = imagecolorallocate($canvas,255,105,180);
$white = imagecolorallocate($canvas,255);
$green = imagecolorallocate($canvas,132,135,28);

// Draw three rectangles each with its own color
imagerectangle($canvas,50,150,$pink);
imagerectangle($canvas,45,60,120,100,$white);
imagerectangle($canvas,75,160,$green);

// Output and free from memory
header('Content-Type: image/jpeg');

imagejpeg($canvas);
imagedestroy($canvas);
?>

laravel中的错误输出

����JFIF``��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90),default quality ��C    $.' ",#(7),01444'9=82<.342��C  2!!22222222222222222222222222222222222222222222222222����"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?���(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��(��+cA�l�5�翽�����\�Al'v̱�)te9�@�[+ 7L'I����ݥ�I�l��H��"�aGj��V���o����㵻�ZxL�h���h(�m�iG�/'�'���s���c��u��C��T�6��v^]e�S�ճ����.?��ڶ��������c��u��C��T}�����?�M�%Qr}����ȧ��g�@ 7��\��?�l�����ˏ�;W>������&��������;����J���e�%�E?�[?�i���������g�@ 7��\�ڹ�?��w\��4?��G���A�s����Uk/�/�)�j���M����v��[?�i�������ϱ�?����� �����t��O�zO��}��[o7n����d�8�2h�{YI�'����4���q��h�ճ����.?��e�E����K��� �u��m�/�[�H��r�D�n���:���M�B2�ו����Yt1�� �?

请让我知道如何在Laravel框架中运行它。谢谢!

解决方法

您不能在刀片/视图上使用标题。您应该直接在控制器/路由上创建它。

路线示例:

Route::get(
    '/test/img',function () {

header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200,200);

// Allocate colors
$pink = imagecolorallocate($canvas,255,105,180);
$white = imagecolorallocate($canvas,255);
$green = imagecolorallocate($canvas,132,135,28);

// Draw three rectangles each with its own color
imagerectangle($canvas,50,150,$pink);
imagerectangle($canvas,45,60,120,100,$white);
imagerectangle($canvas,75,160,$green);

// Output and free from memory
header('Content-Type: image/jpeg');

imagejpeg($canvas);
imagedestroy($canvas);

    }
);

默认情况下,视图/刀片中的内容类型标题将被替换。但是,如果您仍然想实际使用刀片,则可以在刀片文件末尾使用die函数。

刀片示例

<?php

    header("Content-type: image/png");
// Create a 200 x 200 image
$canvas = imagecreatetruecolor(200,$green);

// Output and free from memory
header('Content-Type: image/jpeg');

imagejpeg($canvas);
imagedestroy($canvas);

die;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。