Q:

PHP create image from text and save

belongs to collection: PHP Programming Exercises

0

PHP create image from text and save

In this exercise, you will learn how to create an image from the text using PHP. 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

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 the some PHP predefined functions used for generating images 

imagecreatetruecolor()

This function is used to create an image of the specified size. It takes width and height as parameters.

imageceatetruecolor(int $width, int $height);
 

imagecolorallocate()

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

imagefilledrectangle()

This function is used to draw a filled rectangle with a given start and end coordinates. It takes six parameters. In first parameter, it takes image resource and in next four parameters, it takes the rectangle coordinates and in last parameter, it takes the color code to fill the rectangle.

imagefilledrectangle(resource $image, int $x1, int $y1, int $x2, int $y2, $fillcolor );

imagestring()

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 that can be from 1 to 5 (smaller to bigger), third and fourth parameters are the coordinates from the left corner and fifth parameter is the string to be written and sixth parameter contains string color.

imagestring(resource $image,  int $font , int $x , int $y , string $string , int $color );

imagesetthickness()

The imagesetthickness() function is used to set the thickness of line drawing. In the first parameter, it takes image resource and in the second parameter it takes thickness size in pixels.

imagesetthickness(resource $image,  int $size);

imagepng()

The imagepng() function is used to generate image in png format.

imagepng(resource $image);

imagedestroy()

The imagedestroy() function is used to at last to free the allocated memory associated with the image.

imagedestroy();

PHP Generate Image From Text Example

Here is a very simple example 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to get data from XML file in PHP... >>
<< Write a program in PHP to read from directory...