Building a PHP script to create new image - php

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

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.

PHP GD Image generation broken

I need help in my PHP image code. I'm trying to make Captcha, but all I get is broken image icon ?
This is my code:
<?php
header('Content-type: image/jpeg');
$text = "1111";
$font_size = 30;
$image_width = 110;
$image_height = 40;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpg($image);
?>
"font.ttf" is next to my php file, and names are matching
Use
imagejpeg($image);
Instead of
imagejpg($image);
As imagejpg() doesn't exist.
When debugging you should comment out the header to see the errors.

Move uploaded file php not working

I want to move a file that has been created using imagettftext and saved as a png. as you can see, in the code below, i used the move_uploaded_file but to no avail. please help.
tq
// Set the content-type
header('Content-Type: image/png');
// Create the image from created image
//$im = imagecreatetruecolor(180, 180);
$im = #imagecreatefromjpeg('poloroid.jpg');
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
//imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
//$text = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = '/uploaded_files';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
$name ="$fbid.png";
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
move_uploaded_file($name,"$uploads_dir/$name");
imagedestroy($im);
You are not uploading a file, you are generating one!
imagepng has filename parameter so you can save it to your drive:
$uploads_dir = '/uploaded_files/';
$name = $uploads_dir.$fbid.'.png';
imagepng($im,$name,9);
imagedestroy($im);
try to use rename instead of move_uploaded_file

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.

Outputting image with underlined text using php GD library

What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);

Categories