如何在没有任何外部库的情况下从Laravel PHP中的文本创建图像

如何在没有任何外部库的情况下从Laravel PHP中的文本创建图像,php,laravel,Php,Laravel,我用这段代码从文本在核心PHP中创建图像 header ("Content-type: image/png"); $text='test@example.com'; $string = $text; $font = 3; $width = ImageFontWidth($font) * strlen($string); $height = ImageFontHeight($font); $im = @

我用这段代码从文本在核心PHP中创建图像

header ("Content-type: image/png");
$text='test@example.com';
$string = $text;                                            
$font   = 3;
$width  = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);

$im = @imagecreate ($width,$height);
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, 0, 0,  $string, $text_color);
echo imagepng ($im);
上面的代码在核心PHP文件中运行良好,但现在我正在laravel框架中进行项目工作。当我在laravel中尝试相同的代码时,它不起作用。我试图改变反应方式,但还是失败了 这是我试过的

public function imgCreate(){
    header ("Content-type: image/png");
    $text='test@example.com';
    $string = $text;                                            
    $font   = 3;
    $width  = ImageFontWidth($font) * strlen($string);
    $height = ImageFontHeight($font);
    $im = @imagecreate ($width,$height);
    $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
    $text_color = imagecolorallocate ($im, 0, 0,0);//black text
    imagestring ($im, $font, 0, 0,  $string, $text_color);

    echo imagepng($im);
    //return response($im)->withHeaders(['Content-type'=>'image/png']); 
}
所以基本上,我在控制器函数(imgCreate)中粘贴了相同的代码。我尝试了不同的响应方法,但似乎都有错误 当我像这样回来的时候

 //echo imagepng($im);
return response($im)->withHeaders(['Content-type'=>'image/png']);
它抛出了一个错误 响应内容必须是实现_toString(),“资源”给定的字符串或对象

当我试着用简单的方法

echo imagepng($im);
//return response($im)->withHeaders(['Content-type'=>'image/png']);  
我得到一个不可读的字符串,如下所示:-

"�巴布亚新几内亚IHDRp K�xPLTE���U��~�艾达�c
0S�� ��Q��������67?>�/�Ð8ۘ��F��3.�%Hn�中国���}����H� �里格茨�0Ncȝm�qss��ǒ%7�1X | | x����L����ripW400#;�^10���伊恩德�B`�1“

是否可以从laravel中的文本创建图像?还是我做错了什么? 据我所知,问题似乎是标题“内容类型”,它负责以图像的形式返回结果


提前谢谢

隔离运行时,代码似乎可以工作。我假设laravel在输出之前发送自定义标题,或者向输出中添加一些内容。两者都会破坏你的形象


解决您的问题:找出浏览器端的内容类型和/或找出laravel是否输出其他内容。例如html代码。

您的代码在独立运行时似乎可以工作。我假设laravel在输出之前发送自定义标题,或者向输出中添加一些内容。两者都会破坏你的形象


解决您的问题:找出浏览器端的内容类型和/或找出laravel是否输出其他内容。例如html代码。

您可以通过将图像编码到
base\u 64

    $text='test@example.com';
    $string = $text;                                            
    $font   = 3;
    $width  = ImageFontWidth($font) * strlen($string);
    $height = ImageFontHeight($font);
    $im = @imagecreate ($width,$height);
    $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
    $text_color = imagecolorallocate ($im, 0, 0,0);//black text
    imagestring ($im, $font, 0, 0, $string, $text_color);
    ob_start();
    imagepng($im);
    $imstr = base64_encode(ob_get_clean());
    imagedestroy($im);
    return view('index',array('data'=>$imstr));
并在视图中查看图像

    <img src="data:image/png;base64,{{ $data }}"/>


启发,您可以通过将图像编码到
base\u 64

    $text='test@example.com';
    $string = $text;                                            
    $font   = 3;
    $width  = ImageFontWidth($font) * strlen($string);
    $height = ImageFontHeight($font);
    $im = @imagecreate ($width,$height);
    $background_color = imagecolorallocate ($im, 255, 255, 255); //white background
    $text_color = imagecolorallocate ($im, 0, 0,0);//black text
    imagestring ($im, $font, 0, 0, $string, $text_color);
    ob_start();
    imagepng($im);
    $imstr = base64_encode(ob_get_clean());
    imagedestroy($im);
    return view('index',array('data'=>$imstr));
并在视图中查看图像

    <img src="data:image/png;base64,{{ $data }}"/>


灵感来源于

是的,我确实这么想,但laravel仍然返回内容类型:text/html;所以我试着改变标题类型,但似乎失败了。是的,我确实这么想,但laravel仍然返回内容类型:text/html;所以我试着改变页眉类型,但似乎没有改变failed@JitendraSoftgrid“Json”用于以Json对象的形式发送响应,而Json对象是完全不可用的context@JitendraSoftgrid“Json”用于以完全脱离上下文的Json对象的形式发送响应