Sat, 4 May 2024


Introduction To PHP GD Image Creation

By: Anton, NetArt Media
Wed, 1 August 2018
Introduction To PHP GD Image Creation

The GD image library for PHP is quite robust and even out of the box offers much in the way of graphic manipulation. There is a bit of a learning curve toward getting to use it efficiently and even I'm not even close to that. However, I do use GD once in a while and am usually quite happy with the results.

I present here a basic introduction to creating random images with GD, which could be upgraded and used for a captcha script perhaps, displaying ads or simply showing relevant information to visitors on a website. I will provide a few reference and guide links at the end of the article for your assistance.


The PHP header function isn't only for redirecting urls and such, it is also used to define an image within the GD library. Under that line we create the image using the necessary syntax. The numbers within the brackets are the height and width of the image. In the event the image cannot be created for some reason - usually due to GD not being installed or configured correctly within the php.ini file - an error will be shown in lieu of the created image.

The next step is to define the background color. I kept things simple for this introductory article, defining a basic white background. GD uses RGB to define colors. Most image editors have the RGB properties available for any color you choose. I use GIMP as a guide for defining colors with GD whenever I code up images with it.

Next, we have the text color, which I defined as black with three 0's to represent it's RGB properties. Following that, I created an array of multiplication equations which will be displayed randomly in a sidebar. Using the shuffle array function we can then define a foreach statement for each of the equations and apply any necessary HTML to them. In this case, I left the equations as they are.

The 'imagestring' in this example is quite basic, yet through further configuration you can define True Type Fonts and control the size of the text, etc., with more control. I kept things simple for this article though, showing how GD will work right out of the box.

The last two lines are imperative if you want the image to show up, as well as keep down the memory usage on the server where the image is being generated. We call the image we have just created, assuring it is a png, which we stated within the header function at the top of the page, and then use 'imagedestroy($img)' to ensure the image is free from memory usage. You can read more on 'imagedestroy' and it's importance .

It is best practice to display the GD created image within HTML by calling to it's file name, as shown below in the following code block. 'gd.php' is where the code block above has been saved to in my case.


I wrapped my image in a defined CSS div class to display it the way I wanted. Below are a few screenshots of what the image looks like, with each refresh of the page showing a different, random array value as expected.




As I mentioned above, a person can take this script and run pretty far with it. The text is actually part of the image, which makes this method perfect for a captcha script. Many people use this method on the net, and with good results.

You could also expand on the array to include urls wrapped around each random image, making for an effective sponsored product link scheme perhaps. The only limit is our imaginations and willingness to try out new things.

I hope this tutorial can be helpful to you in some way. I plan on playing with this code block a bit more to see what I can come up with. You never know, you may see something similar to what I created in this article right here on PHP-Scripting.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox