如何在没有任何外部库的情况下从Laravel PHP中的文本创建图像 [英] How to create image from text in Laravel PHP without any external library

查看:56
本文介绍了如何在没有任何外部库的情况下从Laravel PHP中的文本创建图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码通过文本在Core PHP中创建图像

I am creating image in Core PHP from text with this code

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中尝试相同的代码时,它不起作用.我尝试更改响应方法,但仍然失败 这是我尝试过的.

The code above works fine In core PHP file but now i am working on project in laravel framework .I am totally new to laravel . When i try the same code in laravel it doesn't work . I tried changing response methods but still failed here's what i have tried.

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)中粘贴了相同的代码.我尝试了不同的响应方法,似乎都出错了 当我这样回来

So basically,i pasted the same code in controller function (imgCreate) . I tried different response methods all seem to have an error When i return like this

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

它抛出一个错误 响应内容必须是实现__toString()的字符串或对象,并给出资源"."

当我尝试

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

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

i get an unreadable string like this :-

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

"�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"上,它负责以图像形式返回结果.

Is it possible to create image from text in laravel or not ? or am i doing something wrong ? According to me the problem seems to be header "Content-type" which is responsible for returning result in the form of image.

在此先感谢!!!!!

Thanks in Advance !!!!!

推荐答案

您可以通过将图像编码为base_64

You can achieve this by encoding image to 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));

并在您的视图中查看图像

And to view image in your view

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

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

这篇关于如何在没有任何外部库的情况下从Laravel PHP中的文本创建图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆