Dynamic image creation using PHP

As well as being great for generating textual content dynamically, PHP is also useful for dynamically creating images. Using a special PHP extension, we'll explore the possibilities of image creation in this column over the next few columns.

To start with, you should recompile PHP with the graphics extensions we will need to use. They are: gd-1.8.4, Thomas Boutell's excellent graphics library; jpeg-6b, a JPEG manipulation library; and t1lib, a PostScript Type1 font library. Unpack each archive, compile and install based on the instructions included with each package.

Python vs. PHP: Choosing your next project's language

Once they are installed, you will need to recompile PHP. To do so, change to the directory where the source is installed and run the following command:

./configure --with-gd --with-jpeg-dir=/path/to/libjpeg --with-t1lib --with-zlib-dir=/path/to/libz --with-apxs=/path/to/apache/apxs

You may be wondering how to determine the 'path to' information. Using the UNIX 'locate' utility, you will be able to find the path. For example:

$ locate libjpeg
/usr/lib/libjpeg.so

This means that the argument to --with-jpeg-dir will be /usr/lib/. Additional explanation on configuring PHP can be found in the INSTALL file.

Once the new version of PHP is installed, restart your Web server.

Digital images

Unlike text, images are generally represented in a predefined binary encoding, which maps which colours occur where on a two-dimensional grid. The two dimensions of this grid are marked by 'x' and 'y' axes - where 'x' refers to the horizontal dimension and 'y' refers to the vertical.

A pixel is a single position on this map. Programmers refer to a position in terms of its x/y co-ordinates. We will use the mathematical convention of (x,y) to mean 'the position at x-pixels across and y-pixels down'. The pixel in the top-most left-hand corner is position (0,0).

Pixels are simply points of colour. All colours are made of proportions of the primary colours: red, green and blue. The PHP image functions support 256 shades of each of these colours. As such, the number of combinations of red, green and blue supported are approximately 1.68 million and the image resolution is said to be 24 bit.

Creating a basic image

By default, PHP tells the Web browser to expect HTML. When sending an image, we must override this value using the header() function. The following script outputs a basic JPEG image, and tells the Web browser what to expect.

<?
header("Content-type: image/jpeg");
$im = imagecreate(100,100);
$bg = imagecolorallocate($im,255,255,255);
$fg = imagecolorallocate($im,100,120,130);
$borc = imagecolorallocate($im,0,0,0);
imagefill($im,0,0,$bg);
imagerectangle($im,0,0,99,99,$borc);
imagestring($im,1,20,45,"Hello World",$fg);
imagejpeg($im);
imagedestroy($im);
?>


The script sets the new content type on its second line. This content type is a valid MIME type and tells the browser that the content is an image of type JPEG.

The third line creates the PHP image object or 'palette', $im. Three different colours are created, one for the background ($bg), one for the foreground ($fg), and one for the border ($borc).

The second, third and fourth arguments to imagecolorallocate() represent the proportions of red, green and blue, respectively.

The script then 'fills' the image with the background colour $bg (white), starting at the top left hand side (0,0). We then draw a rectangle on the image, starting at (0,0) and ending at (99,99). Since the palette is 100 pixels wide and 100 pixels high, the border follows each edge of the map.

The next line of the script draws the string 'Hello World' horizontally beginning at the position (20,45). The string is written in the colour $fg, using the built-in font '1'. There are five built-in fonts, enumerated from 1 to 5.

Experiment with these different fonts to see what they look like, but also to familiarise yourself with PHP's image functions (you will need to increase the size of the image to fit some fonts).

Finally, the script outputs the image in JPEG format to the browser, using the function imagejpeg(). We then tidy up by calling imagedestroy(), which de-allocates any memory used by the palette $im.

On the next page we'll look at more of the drawing functions as well as using some of the advanced imaging features PHP provides.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

Toys for Boys

Family Friendly

Stocking Stuffer

SmartLens - Clip on Phone Camera Lens Set of 3

Learn more >

Christmas Gift Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Aysha Strobbe

Microsoft Office 365/HP Spectre x360

Microsoft Office continues to make a student’s life that little bit easier by offering reliable, easy to use, time-saving functionality, while continuing to develop new features that further enhance what is already a formidable collection of applications

Michael Hargreaves

Microsoft Office 365/Dell XPS 15 2-in-1

I’d recommend a Dell XPS 15 2-in-1 and the new Windows 10 to anyone who needs to get serious work done (before you kick back on your couch with your favourite Netflix show.)

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Cathy Giles

Brother MFC-L8900CDW

The Brother MFC-L8900CDW is an absolute stand out. I struggle to fault it.

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?