This is an example of using a date to create an actual PNG graphic image.
This is using today's date (default, no variable given in URL):
This is using a specified date in this format: 02/11/2008
View the HTML source to see how the calendar is displayed.
These are the required files used in the script:
The Calendar PNG Image
Arial True-Type Font
Arial Bold True-Type Font
This is the actual PHP script:
<?php
// Calendar Day Cell - Generator
// Get Date from link in the form of mm/dd/yyyy
$date = $_GET['d'];
// If a date is provided, convert to the proper format
if($date)
{
list($month, $day, $year) = explode('/', $date);
if(checkdate($month, $day, $year))
list($month, $day, $year) = explode('/', date('F/jS/Y', mktime(0, 0, 0, $month, $day, $year)));
else
list($month, $day, $year) = explode('/', date('F/jS/Y'));
}
else
{
list($month, $day, $year) = explode('/', date('F/jS/Y'));
}
// Generate Image & set width
$image = imagecreatefrompng("calendar.png");
$imageWidth = imagesx($image);
// Generate Month
$color = imagecolorallocate($image, 255, 255, 255);
$font = "arialbd.ttf";
$fontSize = "8";
$ret = imagettfbbox($fontSize, 0, $font, $month);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 12, $color, $font, $month);
// Generate Day
$color = imagecolorallocate($image, 0, 0, 0);
$font = "arial.ttf";
$fontSize = "14";
$ret = imagettfbbox($fontSize, 0, $font, $day);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 38, $color, $font, $day);
// Generate Year
$color = imagecolorallocate($image, 0, 0, 0);
$font = "arial.ttf";
$fontSize = "10";
$ret = imagettfbbox($fontSize, 0, $font, $year);
imagettftext($image, $fontSize, 0, ($imageWidth - $ret[2]) / 2, 56, $color, $font, $year);
// Output & Free Memory
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>