I installed the GD library this weekend and was able to get it working by sorting through some of the old posts on this forum (much thanks to those who went before!).
I am using it to build a pie graph to show the disk space usage in a directory. My problem is that I am not able to control the background color of my image properly. The best I have been able to do is to make the background the same color as the fill color in my first ImageFillToBorder. I have no idea why this is happening. By moving my ImageColorAllocate commands around, I can get even more seemingly random behavior.
The documentation says that the first ImageColorAllocate after the ImageCreate statement is used as the background color, but that does not seem to be true in my case. Am I doing something wrong?
I am running php4.0.2 from php.net with the php_gd.dll from http://php.weblogs.com/easywindows/. My server platform is Win98/PWS.
Here is the code:
<?php
/*
Description: Generates a JPEG pie graph of disk space
Revisions:
9/24/00 by Mike Fast, initial release
*/
HTML_Head();
Draw_Image();
function HTML_Head() {
Header( "Content-type: image/jpeg");
}
function Draw_Image() {
// Read diskuse file
if ($filename = "w:\diskuse") {
} else {
die ("<p>Error - Could not find the diskuse file.</p>\n");
}
$directory_list = file ($filename);
// Separate directory names and disk usage numbers
foreach ($directory_list as $dir_info) {
$space = split("[\t ]", $dir_info);
$dir_name = $space[1];
$size["$dir_name"] = (int)($space[0]/1024); // Divide by 1024 to convert kB to MB
}
arsort ($size);
Pie_Graph($size);
}
Function Pie_Graph($size_list) {
// Calculate total disk space
foreach ($size_list as $value) {
$sum += $value;
}
// Calculate percentage (of 360 degrees) that each directory occupies
$i = 1;
$arc[0] = 0;
foreach ($size_list as $name => $value) {
$delta[$i] = $value/$sum*360;
$arctotal += $delta[$i];
$arc[$i] = $arctotal;
$dir[$i] = $name;
$i++;
}
$arc[$i] = 360;
// Set image size and color values
$Image = imagecreate(670, 640);
$Background = imagecolorallocate($Image, 255, 255, 255);
$Border = imagecolorallocate($Image, 100, 100, 100);
$TextColor = imagecolorallocate($Image, 0, 0, 0);
$CenterX = 355;
$CenterY = 310;
$Diameter = 580;
// Draw each slice of the pie in a random color
foreach ($arc as $i => $value) {
$End = $value;
$Start = $arc[$i+1];
$Red = rand(150, 250);
$Green = rand(150, 250);
$Blue = rand(150, 250);
if ($arc[$i] < 359.9) {
Pie_Slice ($Image, $CenterX, $CenterY, $Diameter, $Start, $End, $Border, $Fill);
}
$Fill = imagecolorallocate($Image, $Red, $Green, $Blue);
}
// Label each pie slice (greater than 2 degrees) with the directory name
foreach ($dir as $i => $text) {
if ($delta[$i]>2) {
$position = ($arc[$i]+$arc[$i-1])/2+270;
$x = $CenterX + (cos(deg2rad($position))*($Diameter/2*1.02));
$y = $CenterY + (sin(deg2rad($position))*($Diameter/2*1.02));
$line = trim($text);
ImageString ($Image, 3, $x-45, $y, $line, $TextColor);
}
}
// render image
ImageJPEG($Image);
// cleanup memory
ImageDestroy($Image);
}
Function Pie_Slice ($Image, $CenterX, $CenterY, $Diameter, $Start, $End, $Border, $Fill) {
// Readjust 0 degrees to top of pie graph
$Start += 270; $End += 270;
// To draw the arc
imagearc($Image, $CenterX, $CenterY, $Diameter, $Diameter, $Start, $End, $Border);
// To close the arc with 2 lines between the center and the 2 limits of the arc
$x = $CenterX + (cos(deg2rad($Start))($Diameter/2));
$y = $CenterY + (sin(deg2rad($Start))($Diameter/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Border);
$x = $CenterX + (cos(deg2rad($End))($Diameter/2));
$y = $CenterY + (sin(deg2rad($End))($Diameter/2));
imageline($Image, $x, $y, $CenterX, $CenterY, $Border);
// To fill the arc, the starting point is a point in the middle of the closed space
$x = $CenterX + (cos(deg2rad(($Start+$End)/2))($Diameter0.4));
$y = $CenterY + (sin(deg2rad(($Start+$End)/2))($Diameter0.4));
imagefilltoborder($Image, $x, $y, $Border, $Fill);
}
?>