
- Drop me a line! Contact me here.
Starting Out with GD
One of the more useful tools in PHP's box of tricks is the ability to make web graphics on the fly. This is achieved using the GD image library, and a few bits of relatively simple code. There are stacks of possible things that you can use GD for, from resizing uploaded files to creating entirely new images from scratch. The limit is your imagination.
The first thing to realise when you're starting out with GD is that there are two main versions. Theres GD 1.6, and GD 2.x. GD 1.6 is more common on internet hosts as its been around longer and for a long time it was the only version to support GIF images. GD 2.x is the latest version, and has support for things like true colour images, anti-aliasing, and some new functionality like drawing ellipses. For this article we will concentrate on GD 1.6 as the code is then more likely to work with every PHP and GD installation.
As we're using GD 1.6 we have to use the imagecreate() function to start a new image.
This will give us an image 200*200 pixels to work with. If we had GD 2.0 or higher available we could use the imagecreatetruecolor() function. This would mean our image could have 16.7 million colours rather than the 256 colours that imagecreate() is limited to.
In order to draw something we need to assign a colour to our image.
You can also assign a colour to be transparent. This will only work however if you save your image as a GIF or PNG file as the end. JPG files cannot contain transparency. This is a feature of JPGs, not PHP or GD.
To assign a colour to be transparent we can either create a transparent colour, or else we can set one of the colours we have assigned already to be transparent. This is useful if you're working with an image that you have created in a paint program as you can change colours to be transparent in a script rather than saving several images with different transparent colours.
Now that we have an image and some colours to draw with we can get to the business of drawing something. When GD creates an image it starts out with a black background. In this example we need to have a white background instead. There are two main ways to colour the background in. The first is by using the imagefill() function to flood fill the whole area with a single colour.
0,0 is the top left corner of the image. As there is nothing to stop the fill it just floods the entire image with white.
The alternative method of filling the image is to draw a filled rectangle starting at coordinates 0,0 and extending for the entire size of the image.
Both methods of filling the background are equally effective, and just as fast as one another, so its really just a matter of choosing which you prefer to use.
The first thing to realise when you're starting out with GD is that there are two main versions. Theres GD 1.6, and GD 2.x. GD 1.6 is more common on internet hosts as its been around longer and for a long time it was the only version to support GIF images. GD 2.x is the latest version, and has support for things like true colour images, anti-aliasing, and some new functionality like drawing ellipses. For this article we will concentrate on GD 1.6 as the code is then more likely to work with every PHP and GD installation.
As we're using GD 1.6 we have to use the imagecreate() function to start a new image.
<?php
$image = imagecreate(200,200);
?>
This will give us an image 200*200 pixels to work with. If we had GD 2.0 or higher available we could use the imagecreatetruecolor() function. This would mean our image could have 16.7 million colours rather than the 256 colours that imagecreate() is limited to.
In order to draw something we need to assign a colour to our image.
<?php
$black = imagecolorallocate($image,0,0,0);
$white = imagecolorallocate($image,255,255,255);
$red = imagecolorallocate($image,255,0,0);
?>
You can also assign a colour to be transparent. This will only work however if you save your image as a GIF or PNG file as the end. JPG files cannot contain transparency. This is a feature of JPGs, not PHP or GD.
To assign a colour to be transparent we can either create a transparent colour, or else we can set one of the colours we have assigned already to be transparent. This is useful if you're working with an image that you have created in a paint program as you can change colours to be transparent in a script rather than saving several images with different transparent colours.
<?php
$transparent = imagecolortransparent($image);
?>
Now that we have an image and some colours to draw with we can get to the business of drawing something. When GD creates an image it starts out with a black background. In this example we need to have a white background instead. There are two main ways to colour the background in. The first is by using the imagefill() function to flood fill the whole area with a single colour.
<?php
imagefill($image,0,0,$white);
?>
0,0 is the top left corner of the image. As there is nothing to stop the fill it just floods the entire image with white.
The alternative method of filling the image is to draw a filled rectangle starting at coordinates 0,0 and extending for the entire size of the image.
<?php
imagefilledrectangle($image,0,0,200,200,$white);
?>
Both methods of filling the background are equally effective, and just as fast as one another, so its really just a matter of choosing which you prefer to use.
Comments
Comments are not currently being accepted for this article.
Sidebar
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.0358 seconds
