PHP create image from text and save
In this article, you will learn how to create an image from the text using the PHP programming language.
PHP has several built-in functions that are used to create some advanced applications. It also has built-in functions for processing images. In many cases, we need to dynamically convert text to an image, such as when generating CAPTCHA, adding a watermark to an image, creating a logo, or generating some signatures.
Before we get started, we need to have GD extensions enabled. You can quickly check this by using the phpinfo() function. If the GD extension is not enabled, simply open php.ini and search for extension=gd2 and remove the semicolon in front of this.

These are some PHP predefined functions used for generating images.
PHP imagecreatetruecolor() function
This function is used to create an image of the specified size. It takes width and height as parameters.
imageceatetruecolor(int $width, int $height);
PHP imagecolorallocate() function
This function is used to fill the background color of the image, and it returns an identifier for that particular color. It takes four parameters. Image resource in the first parameter, and the other three parameters contain color code in RGB component form.
imagecolorallocate(resource $image, int $red, int $green, int $blue);
PHP imagefilledrectangle() function
This function is used to draw a filled rectangle with the given start and end coordinates. It takes six parameters. In the first parameter, it takes image resource and in the next four parameters, it takes the rectangle coordinates and in the last parameter, it takes the color code to fill the rectangle.
imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, $fillcolor );
PHP imagestring() function
The imagestring() function is used to write text to the image. In the first parameter, it takes the image source, second parameter is the font size, which can be from 1 to 5 (smaller to bigger), third and fourth parameters are the coordinates from the left corner and the fifth parameter is the string to be written and the sixth parameter contains string color.
imagestring(resource $image, int $font , int $x , int $y , string $string , int $color );
PHP imagesetthickness() function
The imagesetthickness() function is used to set the thickness of line drawing. In the first parameter, it takes an image resource, and in the second parameter it takes the thickness size in pixels.
imagesetthickness(resource $image, int $size);
PHP imagepng() function
The imagepng() function is used to generate image in png format.
imagepng(resource $image);
PHP imagedestroy() function
The imagedestroy() function is used at last to free the allocated memory associated with the image.
imagedestroy();
PHP Generate Image From Text Example
Here is a very simple PHP program to generate an image from text and save the generated image in png format.
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$img = imagecreatetruecolor(310, 60);
// Create some colors
$lightsky = imagecolorallocate($img, 135, 206, 250);
$blue = imagecolorallocate($img, 25, 25, 112);
imagefilledrectangle($img, 0, 0, 319, 59, $lightsky);
// The text to draw
$text = 'Welcome to etutorialspoint!';
imagestring( $img, 5, 30, 20, $text, $blue );
imagesetthickness( $img, 10 );
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($img);
imagedestroy($img);
?>
Output

Related Articles
PHP get IP address of visitorImage popup on page load using HTML and jQuery
PHP Contact Form with CAPTCHA and Validation
PHP Graphics: Drawing Line, Rectangle, Polygon
How to insert image in database using PHP
Upload multiple files and store in MySQL database using PHP
multiple choice quiz in PHP and MySQL
Import Data Into MySQL From Excel File
CRUD operations in Python using MYSQL Connector
How to store image in database using PHP
How to send emojis in email subject and body using PHP
PHP7.3 New Features, Functions and Deprecated Functions
Create pie chart using google api
PHP upload multiple files
Most in demand programming languages for 2019
Most in demand NoSQL databases software for 2019
Best mvc PHP frameworks in 2019
PHP MySQL PDO Database Connection and CRUD Operations
Top Android App Development Languages in 2019