Image Generation on the FLY using PHP

Getting Started

Note
To handle the code described in this article, you should feel comfortable with the PHP scripting language at an intermediate level. Also, if you haven't got the necessary libraries to generate images installed (explained in a minute), then you should be able to modify the php.ini file. In our example, we will be using PHP on a Windows 2000 machine running Apache.

To generate images in real-time using PHP, we need to have the GD library installed. To make sure you have the GD library installed, create a new PHP page named checkgd.php in a directory on your web server. Enter the following code into checkgd.php:

<?php
phpinfo(INFO_MODULES);
?>

Run the checkgd.php page on your web server. If you can find a section for the GD library (like in the sample shown below), then you have the GD library installed and ready to go:

On the other hand, if you don't see a section for the GD library, then you will need to download and install the GD library. If you are running windows, then installation instructions are available here. If you are running Linux, installation instructions are available here.

Both of the links to installation instructions shown above use an older version of the GD library. This is because with the newer versions, any support for GIF images has been removed due to copyright infringements.

As mentioned at the beginning of this article, we will be creating an image that contains a random number, similar to the methods used by yahoo.com and paypal.com when you register to become a member.

So, now that we have the GD library installed as part of our PHP distribution, let's jump right in and create our first image.

You might also like...

Comments

About the author

Divyesh Jariwala India

A fellow who believes in simple things, simple way.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold