[英]How to create image from text in Laravel PHP without any external library
I am creating image in Core PHP from text with this code 我正在使用此代码从文本中在Core 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);
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 . 上面的代码在核心PHP文件中可以正常工作,但是现在我正在laravel框架中进行项目。 When i try the same code in laravel it doesn't work .
当我在laravel中尝试相同的代码时,它不起作用。 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']);
}
So basically,i pasted the same code in controller function (imgCreate) . 因此,基本上,我在控制器函数(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']);
It throws an error "The Response content must be a string or object implementing __toString(), "resource" given." 它将引发错误“响应内容必须是实现__toString()的字符串或对象,给出了“资源”。
and when i tried simple method by 当我尝试通过简单的方法
echo imagepng($im);
//return response($im)->withHeaders(['Content-type'=>'image/png']);
i get an unreadable string like this :- 我得到这样的不可读字符串:
" PNG IHDRp K xPLTE U ~ 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 IHDRpK xPLTE U 〜 IDAT c0S 67
0S q 67?> / Ð8ۘ f 3 %Hn cH } H riggc 0Ncȝm qss ǒ%7 1X||x l ripW400#; ^10
10 IEND B`1 “
Is it possible to create image from text in laravel or not ? 是否可以从Laravel中的文本创建图像? 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.
对我来说,问题似乎是标题“ Content-type”,它负责以图像形式返回结果。
Thanks in Advance !!!!! 提前致谢 !!!!!
You can achieve this by encoding image to base_64
您可以通过将图像编码为
base_64
来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 }}"/>
Inspired by https://stackoverflow.com/a/3386050/8317643 受https://stackoverflow.com/a/3386050/8317643启发
Your code seems to work when run isolated. 孤立运行时,您的代码似乎可以工作。 I assume laravel sends custom headers before your output or adds something to your output.
我假设laravel在您的输出之前发送自定义标头,或在您的输出中添加一些内容。 Both can fail your image.
两者都会使您的图像失败。
To solve your problem: find out what the content type is at the browser side and/or find out if laravel outputs something else. 要解决您的问题,请:在浏览器端找出内容类型,和/或找出laravel是否输出其他内容。 Eg html code.
例如html代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.