How to Create an Image Using PHP

How to Create an Image Using PHP

Let’s see how to create an image using PHP. We can create a dynamic image using PHP with GD library enabled. You can create PNG, GIF, JPEG images using PHP. First of all we will discuss key features.

Getting ready to create an image using PHP and GD

What are the requirements?
You need GD library for this. To create GIF images you need a GD version older than gd-1.6 and newer than gd-2.0.28. To create PNG images you need a GD version greater than gd-1.6. So it’s good to have a version newer than gd-2.0.28.

What is this GD library?
GD is an open source code library for the dynamic creation of images. At the beginning GD stood for “gif draw”. But in later versions GIF support was dropped and other formats were supported. So the developers would like to say GD stands for “Graphic Draw” which makes much sense.

How do I use GD library?
Almost every popular hosting provider includes GD library by default to their hosting packages. So you don’t have do any additional installations. But if you wish to use use it in a local environment or manually configured hosting environment you need to download and install it first.

GD is included in WAMP. To enable GD library in WAMP;

Left click on WAMP tray icon –> PHP –> PHP extensions –> Tick php_gd2

or Left click on WAMP tray icon –> PHP –> open php.ini –> Search extension=php_gd2.dll –> If there is a ; in front of the line, remove it. Otherwise GD library is already enabled.

You can download it here. You can find installation documentation here.

What type of image formats can I create using PHP?
Using GD you can create PNG, JPEG, GIF, WebP, XPM, BMP and some other formats.

Create an image

Basically there are 05 steps to generate an image using PHP. We will create a simple image using these 05 steps.

Step 01: Define header type
We must define what type of output we are presenting. In our case as we create images, we define image/format as our content type. Replace format with image format you create. (gif, png, jpeg, etc…). You don’t have to add this line at the beginning of your PHP file. But you must define Content-type before any output. In this case you can add this before Step 04.

Step 02: Setting up the identifier
As in any other image creating process, the first step of creating an image in PHP would be defining the dimensions of the desired image. Using imagecreate(); we basically draw the outlines of a blank image of specified size.

But if you need to get a higher quality image, use imagecreatetruecolor();. It will create the highest quality image with a black background.

Step 03: Define colors and other attributes
An image must have colors. We set it here. For this basic example I’m going to set background color only. But you can set color for other properties too. Color must be set using RGB colors

Step 04: Create the image
Now we create and output the image in the format we needed as final result: imageformat You must replace format with the same format you defined as Content-type here.

Step 05: De-allocate colors and free up the image memory

Complete code to create a simple image using PHP

Generating dynamic images require more memory. Specially for larger images. Make sure to set memory_limit in php.ini is high enough.
Generate images using PHP and GD library

More Image functions in GD library

Now let’s use some other important functions and create an image. After adding all these functions you will get an image like this. Use the complete code at the end and try.
01. Create a transparent image
To create a transparent image you have 02 options.

Option a: Use imagecolorallocatealpha(); to add color. It’s same as the imagecolorallocate(); with an additional parameter alpha. Add a value between 0 and 127. 0 means completely opaque and 127 means completely transparent.

Option b: Use imagecolortransparent(); It’s quite different than the previous one. Instead of adding transparency to the image directly, it makes previously defined color transparent. So you must have set a color first.

02. Draw text on image
To draw text horizontally on a image, we use imagestring(); function and to draw vertically we use imagestringup();.

03. Use TrueType fonts to write
We can use 1-5 as built in fonts in 1-smaller and 5-bigger. If we need to use larger fonts we have to register our own bitmap file using imageloadfont(); function. Now let’s see how to use TrueType fonts (.ttf) since we use them everyday.

04. Draw ellipses, circles and arcs
There are 02 functions to draw circles and ellipses. The original one is imageellipse();.

If you set the width and height to an equal value, it’s a circle. The second option is by drawing a full arc using imagearc(); function

05. Draw a rectangle
Use imagepolygon(); function.

06. Draw a polygon
Use imagerectangle(); function.

07. Draw color filled arcs, ellipses, circles, rectangles and polygons.
It’s the same way that we use to draw each particular object. Only add the word filled between image and object name.
Ex: ImageFilledArc();

08. Draw a line
Use imageline(); function.

09. Set the thickness of an object
To set the outline thickness of an object like a line, ellipse, rectangle, etc… in pixel use imagesetthickness(); function. You must call it right before you draw the object you need to apply thickness.

10. Fill an image with given color
Use imagefill(); function.

Complete code to create an image using more functions

No Responses