Image Output - Squares
So far, all of the scripts we have written are designed to output HTML. PHP can output other types of information, like image files. PHP is open source software. This has allowed a number of different third parties to develop image libraries. There are several libraries that could be used to do this. A commonly-used image library is the GD library which has sufficient functionality for most purposes and can output images in a variety of formats.
The following code produces a PNG image, 100x100 pixels, of a square.
$im = @imagecreate(100, 100);
$red = imagecolorallocate($im, 255, 0, 0);
$green = imagecolorallocate($im, 0, 255, 0);
imagefilledrectangle($im, 0, 0, 99, 99, $green);
imagerectangle($im, 0, 0, 99, 99, $red);
You can see the image that this produces by saving the file with a PHP extension and typing in its URL in the browser. You should simply see the image that is created by this script.
Alternatively, you can use the URL of the script as the src for an image in an HTML file. For example, if you had called the script square.php, you could have a line like the following in a different HTML page,
<img src='square.php' alt='image of a square'>
Image library documentation can be found at http://www.php.net/manual/en/ref.image.php. Make sure you can change the size and colour of the squares before moving on.