This is a detailed tutorial on how you can add text to image in PHP using the GD library. It allows writing any text with any font on any image background.
This is also one of my favorite topics in PHP. This thing has once helped me in generating thousands of images for a website in no more than a few minutes of time. Moreover, it is a highly required demand for those websites and other web development projects that requires the feature of dynamic image generation. This technique allows you to create images only when they’re required to be served somewhere and helps to save a lot of server space.
Writing Text On An Image Using PHP GD Library
The code to write text on an image in PHP is really simple.
//Text To Add
$text = "WTMatter.com Tutorial";
//Background Image - The Image To Write Text On
$image = imagecreatefrompng('board.png');
//Color of Text
$textColor = imagecolorallocate($image, 229, 85, 78);
//Full Font-File Path
$fontPath = 'C:\xampp\htdocs\arial.TTF';
//Function That Write Text On Image
imagettftext($image, 60, 15, 225, 425, $textColor, $fontPath, $text);
//Set Browser Content Type
//Send Image To Browser
//Clear Image From Memory
The above-specified code will add the text, WTMatter.com Tutorial, on an image, board.png. The output of the above code is shown in the screenshot given below.
This code picks up a PNG image as the background and then writes the specified text at the desired location with the provided size, color, and angle.
Understanding The Code
Now, let’s understand each line of the code.
In the first line, we’re just storing the text to be printed in
In the second line, we’re using the PHP function
imagecreatefrompng() to create an image object from an already existing PNG image board.png. The full name of the image file which is saved in the same location as of the PHP file is provided as a string parameter to this function.
You can also use other functions like
imagecreatefromgif() or simply
imagecreatefromstring() to load an image into the
$image variable from any image format.
In the next line, the function
imagecolorallocate() returns the RGB specific color that can be used for the current image. Therefore, this function takes four different arguments, the first one is the $image variable itself and the next three parameters are respectively the RGB numbers for the text color. You can find out the RGB color value for any color using this calculator. So, upto here we’ve stored a red shade color in the
Now, in the next line, we’re simply storing the full path of the font-file in the variable
$fontPath which should be used for the text.
The fifth line is the most important line which is actually writing the text on the image. This work is being done by the
As specified in the above image this function takes 8 different arguments. The arguments in order are defined below.
|Argument Number||Argument Name||Specified As Per Our Code|
|1||Resource (The Image)||$image|
|3||The angle at which text will be inclined||15|
|4||Horizontal Text Start Position||225|
|5||Vertical Text Start Position||425|
|6||Image Allocated Text Color||$textColor|
|7||Full Font File Path||$fontPath|
|8||Text To Be Printed||$text|
Once this function executes successfully, the created image stores itself in the resource variable
Before you can actually display the image in the browser, you must set the header type of the image, otherwise, the un-readable image text code will be printed in your browser window. You can easily set header for the image using the
header() function in PHP.
The second last line is the actual command to send the image to the web browser. In our code, we’ve just given the
$image resource variable as the argument to the
If you want to save this file instead of sending it to the web browser, you just have to pass one from the argument, which is the name of the file to be saved. The following line of code will save the produced image as the filename test.png.
You can also use other functions like
imagejpeg() to produce a JPEG image.
Now, if you know PNG is a lossless compression file format but JPG is a lossy file format. So, you can also play with the imagejpeg() function to change the quality and therefore the size of the output image as well. Isn’t it sound interesting? Let us check.
Find out more about JPG and PNG images at the following link.
imagejpeg($image,"test.jpg",10) will save the image as test.jpg with 10% of the full image quality. Similarly, you can serve the different quality of JPG image into the browser without saving it by providing the file name argument of this function with the null value.
In our case, the size of the full quality PNG image is 483 KB. On the other hand, the size of JPEG image without specifying the quality parameter is 67 KB but with specifying the quality to be 10%, it is 27KB.
In the end, you should free the memory occupied the image resource variable and that’s what we did in the last line of our code. This is a good practice to keep your web server running smoothly and efficiently.
Note. Image Creation and text addition work pretty well in PHP, but I’ve seen some issues with it when working with other languages using several other Unicode Characters than the English Language. One of the most common issues that I faced is the improper rendering of Devnagri text. Later after research, I used the Python Language solution to add Devanagari text on images and it works pretty well. For that, you need to use PyVips module with libvips image processing library.
I hope this article has helped you in adding text to images in PHP. Do share it with others who might find this useful as well. Feel free to ask any kind of questions related to this article in the comments section. Also, let us know if you find this thing interesting and do you know any other ways of generating images with text in PHP.
- Remove Background From Image in 5 Seconds
- 5 Best WordPress Image Compression Plugins
- Fix WordPress Memory Exhausted Error [Increase PHP Limit]
- PHP Redirect – How To Redirect With PHP header() Function?
- How to Install WordPress on XAMPP? (You can run your local PHP Server With XAMPP)