简体   繁体   English

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

[英]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_64base_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.

相关问题 如何在不使用任何库的情况下用php创建JSON rest webservice? - How to create a JSON rest webservice in php without using any library? 如何在不使用任何excel库PHP的情况下创建xlsx文件 - How to create xlsx file without using any excel library PHP 如何在没有任何外部数据库的情况下将 mysql 与 laravel 一起使用 - how to use mysql with laravel without any external database 如何在Laravel 5中加载外部PHP库(带有多个文件夹)? - How to load an external PHP library (with multiple folders) in Laravel 5? 如何使用 PHP GD 库向图像添加文本 - How to add text to an image with PHP GD library 如何在没有任何 php 脚本的情况下上传图像? - How to upload image without any php script? 如何使用GD Library在PHP中创建缩略图图像而不失去其原始质量 - How to create Thumbnail Image in PHP without loosing its Original Quality using GD Library 如何在不破坏任何代码的情况下从旧的PHP数据库处理程序库迁移到PDO? - How to migrate from old PHP database handler library to PDO without any code breaking? 使用PHP调整图像大小,但不使用库 - Resize an image with php without a library 如何在php中使用自定义文本创建图片? - How to create Image with custom text in php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM