turning text into image - PHP/GD - save image - php

I'm using this script to simply create an image from text. What I would like to know is how to save the image instead of printing straight to browser;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
$image = imagestring ($im, $font, 0, 0, $string, $text_color);
I know its probably just one line of code at the end but im not sure which GD function to use.
Any help would be much appreciated, Regards, Phil.
EDIT:
I have tried the following but just get a balnk black box;
// an email address in a string
$string = $post[$key];
// some variables to set
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
// lets begin by creating an image
$im = #imagecreatetruecolor ($width,$height);
//white background
$background_color = imagecolorallocate ($im, 255, 255, 255);
//black text
$text_color = imagecolorallocate ($im, 0, 0, 0);
// put it all together
imagestring ($im, $font, 0, 0, $string, $text_color);
imagepng($im, 'somefile.png');
imagedestroy($im);

Pass a filename to the appropriate image-generating image*() function:
imagepng($image, 'somefile.png');

Look at here http://www.php.net/manual/en/function.imagepng.php

take a look at imagepng() (or imagegif, imagejpeg...) - if you add a filename as second parameter, the image is saved as file.

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 generate a text into an png without spacing (top and bottom)

I am trying to write a php function to convert text (via imagettftext) into an image (png) cropped from the top to the bottom of the letters.
I've tried with GD (php) and html2canvas (javascript)
But the generated image has always a spacing around the text. The text is contained in a for the height and line-height...
Try this,
<?php
header ("Content-type: image/png");
$text='CONVERT ME TO AN IMAGE';
$string = $text;
$font = 3;
$width = ImageFontWidth($font) * strlen($string); // minimum width required to hold the font
$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);
?>

Building a PHP script to create new image

I'm on my way to create a new script for my personal website. I want some kind of function where visitors can write something in an input field and then press generate image.
The script should then take a background image and insert the visitors text on top.
My question is: What is the php code to actually generate the image file and how do i connect the input data to the image?
I do not need full code examples if you don't have the time, i just need the basis practice of how to setup such a system.
Try this code:
$filename = "layout.jpg";
$im = #imagecreatefromjpeg($filename);
$font = "tahoma.ttf";
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, 15, 0, 50, 50, $black, $font, $_POST['message_from_user']);
header('Content-Type: image/jpeg');
imagejpeg($im, null, 100);
imagedestroy($im);
layout.jpg is background image, tahoma.ttf is font file
Both files should be placed in the same folder.
This code would generate jpg-image with user string.
More details you would get by searching "gd in php".
do something like this:
$text = 'put ur txt here';
$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, 'file name here', 80);

how to generate multiple texts using php

How can i generate multiple texts using the imagecreatetruecolor() method? I have the following code, but this displays either the first font or the second - not both:
<?php
// Set the content-type
header('Content-type: image/png');
// The text to draw
$text = 'Hello Farooqi';
$x = 0;
$y = 0;
$w = 50;
$h = 50;
$s = 13;
// Create the image
$im = imagecreatetruecolor($w , $s);
imagesavealpha($im, true);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im,0,0,0);
$text_color = imagecolorallocate($im, 200,200, 91);
$blue = imagecolorallocate($im,0,0,180);
$alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
//imagefilledrectangle($im, 0, 0, 150, 25, $black);
imagefill($im, 0, 0, $alpha);
// Replace path by your own font path
$font = 'Calibri Bold.ttf';
// Add the text
$dimensions = imagettftext($im, $s, 0, $x, $y, $black, $font, $text);
$textWidth = ($dimensions[2]);
$imm = imagecreatetruecolor($w , $s);
imagesavealpha($imm, true);
$bluem = imagecolorallocate($imm,50,50,50);
$alpham = imagecolorallocatealpha($imm, 0, 0, 0, 127);
imagefill($imm, 0, 0, $alpham);
imagettftext($imm, $s, 0, $x+3, $y+3, $bluem, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagepng($imm);
imagedestroy($im);
imagedestroy($imm);
?>
Here above in these last 4 lines only one line appears, and that's the first one. How can I have both lines?
Please help. Thanks in advance.
The imagepng($im); will be called and outputted to your HTML code and as the header is set to an image it will display this image. No matter your imagepng($inm) that comes afterwards.
A better way would be to create two different PHP files. One that does your script and ends in imagepng($im); and another one that ends in imagepng($inm);
And then in your master PHP (header = text/html) file you just mention these 2 files in your image source:
<img src="functions/first_image.php" />
<img src="functions/second_image.php" />

not Display text

<?php
header ("Content-type: image/pjpeg");
$string = "manujagadeesh!!!";
$font =8;
$width = ImageFontWidth($font)* strlen($string) ;
$height = ImageFontHeight($font) ;
$im = ImageCreateFromjpeg("sachin.jpg");
$x=100;
$y=200;
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, $x, $y, $string, $text_color);
imagejpeg ($im);
?>
this is the add text to image in php
wen we inclue the my html page the text is not displaying
for eg
<?php
header ("Content-type: image/pjpeg");
$string = "manujagadeesh!!!";
$font =8;
$width = ImageFontWidth($font)* strlen($string) ;
$height = ImageFontHeight($font) ;
$im = ImageCreateFromjpeg("sachin.jpg");
$x=100;
$y=200;
$background_color = imagecolorallocate ($im, 255, 255, 255); //white background
$text_color = imagecolorallocate ($im, 0, 0,0);//black text
imagestring ($im, $font, $x, $y, $string, $text_color);
imagejpeg ($im);
?>
hi welcome
couldn't see the hi wlecome
You can't output both content of type image/jpeg and text/html from the same PHP script. Move your image generation code to another file eg stringImage.php and use the following code to include it:
<img src="stringImage.php" />
hi welcome
As long as stringImage.php only outputs the image data with the correct content type, and nothing else, you should get your expected result.

Categories