not Display text - php

<?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.

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);
?>

Gd Library Image Style imagettftext

How to make Like This Image?
I just want to learn how do i get output like this using GD library,
Thanks for your help guys..
Here is your code, you could easily find it on google tho.
<?php
header ("Content-type: image/png");
$string = "your text"; // Change this text
$font = 4; // try changing this as well
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("/path/to/yourimagefile");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
?>
Here's an example:
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
imagepng($image); // outputting the image
For more see imagettftext().
EDIT: An example of using multiple imagettftext():
header('Content-Type: image/png'); // setting the content-type
$file = "yourimage.png";
$image = imagecreatefrompng($file); // creating the image
$font = "YourFont.ttf";
$size = 15; //pixels
$color = imagecolorallocate($image, 255, 255, 255); //white color
$text = "Your text here"
imagettftext($image, 15, 0, 20, 40, $color, $font, $code); // adding the text
$text = "Text 2";
imagettftext($image, 15, 0, 25, 45, $color, $font, $code); // adding the text
$text = "Text 3";
imagettftext($image, 15, 0, 30, 50, $color, $font, $code); // adding the text
imagepng($image); // outputting the image

Create transparent png with text from scratch in php

All the examples I've found on the web seem to create pngs with text from an existing png. Is it possible to create a transparent png from scratch and then add text?
The code ive got so far follows (but it doesnt work. just outputs a blank image source)
<?php
$width = 150;
$height = 30;
$text = "My Text";
$fontsize = 5;
$im = imagecreate($width, $height);
$transcolor = imagecolortransparent($im);
imagestring($im, $fontsize, 0, 0, $text, $transcolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
<?php
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(strlen($string) * $font / 1.5, $font);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, $font - 3, $lime, "droid_mono.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>
Use imagestring instead of imagettftext if you don't want custom font.
Here's the solution based on your original code.
<?php
$width = 640;
$height = 480;
$text = "My Text";
$fontsize = 5;
$img = imagecreate($width, $height);
// Transparent background
$black = imagecolorallocate($img, 0, 0, 0);
imagecolortransparent($img, $black);
// Red text
$red = imagecolorallocate($img, 255, 0, 0);
imagestring($img, $fontsize, 0, 0, $text, $red);
header('Content-type: image/png');
imagepng($img);
imagedestroy($img);
?>
I Think GD is one of the most popular for generating images, with imagettftext.
<?php
$text = 'SOME TEXT';
$font="c:/windows/Fonts/Latinwd.ttf"; //Load font file for windows
$im = ImageCreate(700, 140);
$bla = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $bla); //transparent background
$black = imagecolorallocate($im, 255,255,255);
ImageTTFText ($im, 38, 0, 10, 40, $black, $font, $text);
header('Content-Type: image/png');
ImagePNG($im, 'name.png');
imagedestroy($im);
?>

turning text into image - PHP/GD - save image

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.

Categories