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.
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.
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #98: The Future of Datacentre IP 18/12/2008 10:33:00
CW Live speaks withLin Nease, Director of Emerging Business for HP ProCurve, to discuss the future of networks, including the effect of IP-based storage on datacentres, new capacity requirements generated by the use of 10Gb Ethernet, and how an efficient network design can slash energy and cooling costs, and help enterprises build a "green" image. - +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport.
IT industry veteran advises caution on outsourcing selection in light of Satyam problems 2009-01-09 21:45:00+11
F-Secure Warns About a Worm Affecting Corporate Networks 2009-01-08 16:42:00+11
Research software developer appoints Susan Dart to new Business Development Director role 2009-01-08 09:08:00+11
Research software developer appoints Susan Dart to new Business Development Director role 2009-01-08 09:08:00+11
Anyware Introduce Two Powerful PCI TV Tuner Cards with S5 Power Up and Windows Media Center Remote 2009-01-07 17:30:00+11
Strategies for Eliminating .PST Files
Join industry expert Martin Tuip to discover best practice strategy for the archival and removal of .PST files using email archiving. Learn how to ensure long-term email records are there when needed, and reduce the risk to your business and clients.





