PHP: Generate png from text on the fly - php

I wrote this function to create png file from text :
function pngfromtext($text){
$fontsize = 5;
$width = imagefontwidth($fontsize)* strlen($text);
$height = imagefontheight($fontsize);
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 255, 255);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
}
I put codes to functions.php file, When use this function in another page I get this error:
Warning: Cannot modify header information - headers already sent by (output started at ..\functions.php on line 58
�PNG IHDRZ^%JPLTE����ٟ�tRNS#��f�IDAT�c` Hȱ�7�H��'��`c��s�����i��$���Hl`8��Ɛ�� ��#�c��p�� q�3f�òm�� �g�ـ�6fF ���h�bc�sXd4c�A4����?|�¦����r+���!IEND�B`�
What is wrong?

Set the header at the point where you know that your output is going to be an image. This means set this statement
header('Content-type: image/png');
at the beginning of your php script.
There is also a possibility that the header-command was already executed before this point here.

You can't use multiple header in on page.

before header('Content-type: image/png'); put ob_clean();
that cleans the response object so you can add the headers again

Related

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
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 . 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']);
}
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']);
It throws an error
"The Response content must be a string or object implementing __toString(), "resource" given."
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�c0S �� ��q��������67?>�/�Ð8ۘ��f��3�%Hn�cH���}����H� �riggc�0Ncȝm�qss��ǒ%7�1X||x����l����ripW400#; �^10���IEND�B`�1"
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 !!!!!
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 }}"/>
Inspired by 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. 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. Eg html code.

How to convert text to image in PHP with line break?

Currently I am using the PHP code below to convert string text to image with transparent background. Now I need to convert the string text to image with line break, transparent background and also with custom font. Code I am using:
$width = 200;
$height = 40;
$text = "Test text -- line break here -- with line break";
$fontsize = 3;
$img = imagecreate($width, $height);
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
$color = imagecolorallocate($img, 0, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $color);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
Doing it like this will help you
header("Content-type: image/png");
$str1= 'Test text ';
$str2= 'with line break';
$image= imagecreate(200,40);
$background = imagecolorallocate($image,255,255,255);
$color= imagecolorallocate($image,0,0,0);
imagefill($image,0,0,$background);
imagestring($image,10,5,5,$str1,$color);
imagestring($image,10,5,20,$str2,$color);
imagepng($image);
output look like this

Create Image in PHP without browser output

I want to create an image with text and store in temp without output to browser
I have used the following code:
header("Content-type: image/jpeg");
$imgPath = 'images/certificate.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = $userid;
$fontSize = 3;
$x = 115;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image,'images/certi.jpg');
imagedestroy($image);
When using imagejpeg to save a file, you do not need the header header("Content-type: image/jpeg"); line
If you are not returning image data, then you should not be setting the header.
Remove the header("Content-type: image/jpeg");
I'm not clear on exactly what you're trying to do, but if the work has been done, there is no need to return any output, if output is not desired.

captcha displaying in a funny way

I have a captcha Code which works perfectly but I really dont know why it displays funny.
Can anyone help me figure out what might be the cause of this? Here is the code:
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
This is what is displays:
kX’OýÓx£Ç> ñ=¨µÖ/b{ct÷¯½¬6É-ÀiH¡ä csdòyäæcÆ^"Ö|7¥è¦­ss¤i™û%³‘¶>02q–Ú2q;A!p+Ÿ¢€:¿øX>'þÂþÈþÓÿDûöo™öx¾Ñö]Û¼?o›ågø7mÇËŒqF‡ñÄú•Ÿ¦j~M½·öWkx¤šÓÎ]²ù²™!Ü:ìeä“Ô“\¥QEQEÿÙ
You're missing the header. Add the following after session_start():
header('Content-Type: image/jpeg');
The output will be something like:
When you're creating an image like this, you need to tell the browser what type of content you're wanting to display. Hence the reason:
header("Content-type: image/jpeg");
is needed. Read the Wiki article on MIME if this confuses you. You'll also find that you'll need to specify the content type whenever you're generating other file types as well (for example: PDF files generated on the fly with PHP will require header('Content-type: application/pdf'); )

How to draw a vertical line on image in php [duplicate]

This question already has answers here:
PHP GD how to draw text over a line
(3 answers)
Closed 9 years ago.
i am making an antibot that displays different pictures, and asks the user to click on a specific picture. However, i would like some small random change to be made to the picture each time, so that software could not analyze and determine which picture is beeing displayed...
I would like one horizontal and one vertical line to be added at random coordinates with random color to the picture each time, then display the picture using get_file_contents and header.
Hope this makes sense... I would not want the changes to be saved to the picture, but only displayed to the user... I am using file_get_contents and header to display the picture, like this:
$id = $_GET['id'];
$image = "images/".$id . ".jpg";
$content = file_get_contents($image);
header('Content-Type: image/jpeg');
echo $content;
exit()
Thanks...
Using GD within PHP would allow you to do this
http://php.net/manual/en/book.image.php
create a file called image.php
From the php manual:
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
You could add a line by using the imageline function
<?php
header("Content-type: image/png");
$string = $_GET['text'];
$im = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imageline($im, $x1, $y1, $x2, $y2, $orange);
imagepng($im);
imagedestroy($im);
?>
There are so many PHP GD functions available to achive this I think below code snippet may help you
<?php
$im = imagecreatefrompng("images/yourImage.png");
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
// Draw a vertical dashed line
imagedashedline($im, 50, 25, 50, 75, $white);
// Save the image
imagepng($im, './imagewithdashedline.png');
imagedestroy($im);
?>
reference PHP GD imagedashedline function

Categories