【问题标题】:How to create image from text in Laravel PHP without any external library如何在没有任何外部库的情况下在 Laravel PHP 中从文本创建图像
【发布时间】:2018-04-07 05:32:21
【问题描述】:

我正在使用此代码在核心 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 完全陌生。当我在 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()的字符串或对象,“resource”给定。”

当我尝试简单的方法时

echo imagepng($im);
//return response($im)->withHeaders(['Content-type'=>'image/png']);  

我得到一个像这样的不可读字符串:-

".PNG IHDRp K.xPLTE..U......~.IDAT.c0S �� ��q��������67?>�/�Ð8ۘ��f��3�%Hn�cH���}����H� �riggc�0Ncȝm�qss��ǒ%7�1X||x����l����ripW400#; �^10��.IEND.B`.1"

是否可以从 laravel 中的文本创建图像?还是我做错了什么? 在我看来,问题似乎是标题“Content-type”,它负责以图像的形式返回结果。

提前致谢!!!!!!

【问题讨论】:

  • @JitendraSoftgrid "Json" 用于以完全脱离上下文的 Json 对象的形式发送响应

标签: php laravel


【解决方案1】:

您可以通过将图像编码为base_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 }}"/>

灵感来自https://stackoverflow.com/a/3386050/8317643

【讨论】:

    【解决方案2】:

    您的代码在独立运行时似乎可以工作。我假设 laravel 在您的输出之前发送自定义标头或在您的输出中添加一些内容。两者都可能使您的形象失败。

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

    【讨论】:

    • 是的,我确实是这么想的,但是 laravel 仍然返回 Content-Type: text/html;所以我尝试更改标题类型但似乎失败了
    猜你喜欢
    • 2019-02-04
    • 2017-01-03
    • 2023-01-14
    • 1970-01-01
    • 2018-05-21
    • 2016-03-25
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多