Ooer.com by Chris Neale
A PHP Advent Calendar Using GD

A PHP Advent Calendar Using GD

One of PHP's greatest strengths is its flexibility. Whether you're using it as a simple scripting language to drop database content into a webpage, or writing a full blown content management system, PHP will usually rise to the challenge. And one of PHP's cleverest tricks is its ability to generate images on the fly.

Using the GD graphics library you can build custom web graphics programatically with many very powerful options. You're only limited by your imagination. In this article we're going to step through what it takes to use PHP and GD to build an advent calendar that automatically updates itself each day the image is viewed.

The first thing that is required in any advent calendar is a background image.

<?php
$advent
= imagecreatefromjpeg("advent.jpg");
?>

When an image is opened in PHP it can be assigned to a handle. In this case we're using $advent. At this point we should also realise that GD 1.6 is not the most up-to-date version. 1.6 does not handle "true colour" images. If your background image contains more than 256 colours GD will dither it. If you are using GD 2.0 or higher this will not happen, however most web hosting companies are reluctant to upgrade to the newer version as support for GIF images was lost.

<?php
for ($y=0;$y<5;$y++) {
for (
$x=0;$x<5;$x++) {
++
$counter;
$x1 = (($x*$wsize)+($x*$xspace)+$xoffset);
$y1 = (($y*$wsize)+($y*$yspace)+$yoffset);
$x2 = (($x*$wsize)+($x*$xspace)+$wsize+$xoffset);
$y2 = (($y*$wsize)+($y*$yspace)+$wsize+$yoffset);
imagerectangle($advent,$x1,$y1,$x2,$y2,$white);
imagestring($advent,2,$x1+5,$y1+2,$counter,$white);
}
}

?>

Drawing the windows for your advent calendar requires a loop. I have used two loops here, both with 5 iterations each. This makes the maths simpler, and, as there are 5 rows and 5 columns, it makes sense (in my head).

During the process of drawing the windows we should also check to see if the date has already gone. If it has then we need to draw an image in the box rather than leave it blank.

<?php
if (date("d") >= $counter and date("m") == 12) {
imagefilledrectangle($advent,$x1,$y1,$x2,$y2,$lightgrey);
$window = imagecreatefromjpeg($counter.".jpg");
imagecopy($advent, $window, $x1+(($wsize-imagesx($window))/2), $y1+(($wsize-imagesy($window))/2), 0, 0, imagesx($window), imagesy($window));
}
?>

The key command is imagecopy(). Once we open the image we want to use for the window picture we copy the content of it into the $advent resource. This overwrites the content there previously filling in the advent calendar window. By looping through each day we fill in all the days until Christmas.

Obviously there is more to writing a working advent calendar than just making a dynamic image. You need to gather various items of information about the date to begin with and work out an efficient way of calculating the location of the image windows. Also a system to cache the image and only generate a new one if it is actually needed would help reduce the web server load considerably. Generating images on the fly is quite CPU intensive.