Location: PHPKode > projects > phpBandwidth > phpbandwidth/bw_image.php
<?
// phpbandwidth by Alexander Wigen
if (!is_numeric($uid)){
	header("Location: index.php");
	exit;
}
include("db_config.php");
include("db_$db[type].php");
include("user_functions.php");


$conn = @db_connect();
if ($conn === FALSE){
	echo "Cant connect to the database, have you connfigured it at <a href=\"db_install.php\">db_install.php</a> ?";
	exit;
}
$users = db_get_users($conn);
if (count($users) == 0){
	?>No users defined!<br><a href="user_add.php">Add user here</a><?
	exit;
}
if ($heigth > 60)  // If image is taller than 60 pixels, traw a double bandwidth graph line
	$brush = @imagecreate (2,2) or die ("Cannot Initialize new GD image stream");
	else
	$brush = @imagecreate (1,1) or die ("Cannot Initialize new GD image stream");
$im = @imagecreate ($width, $heigth)
    or die ("Cannot Initialize new GD image stream");

// Allocate the colours we need
$backgroundcolour	= imagecolorallocate ($im, 255, 255, 255);
$black			= imagecolorallocate ($im, 0, 0, 0);
$red			= imagecolorallocate ($im, 255, 0, 0);
$green			= imagecolorallocate ($im, 0, 255, 0);
$grey			= imagecolorallocate ($im, 150, 150, 150);
$brush_colour2 		= imagecolorallocate ($brush, 30, 30, 255);
$brush_colour 		= imagecolorallocate ($brush, 0, 255, 0);

$graph_heigth = $heigth;
$graph_width = $width;
if ($width > 449 && $heigth > 99)
	$big_picture = TRUE;
if ($big_picture){
	$graph_heigth = $heigth - 30;
	$graph_width = $width- 70;
	$graph_offset_x = 65;
	$graph_offset_y = 10;
	imageline ($im,	$graph_width + $graph_offset_x,($graph_heigth/2) + $graph_offset_y,	$graph_offset_x,($graph_heigth/2) + $graph_offset_y, $grey);
	imageline ($im,	$graph_width + $graph_offset_x,($graph_heigth/4) + $graph_offset_y,	$graph_offset_x,($graph_heigth/4) + $graph_offset_y, $grey);
	imageline ($im,	$graph_width + $graph_offset_x,($graph_heigth/4)*3 + $graph_offset_y,$graph_offset_x,($graph_heigth/4)*3 + $graph_offset_y, $grey);
}

// Here comes the header, so we don't want any error messages after this please!
header ("Content-type: image/png");

//fill the brush image
imagefilledrectangle($brush, 0,0,1,1, $brush_colour);
// Activate the brush for all linedrawing with colour IMG_COLOR_BRUSHED
imagesetbrush($im,$brush);
// And a nice box around the graph
imagerectangle ($im, $graph_offset_x-1, $graph_offset_y-1, ($graph_width + $graph_offset_x)-1, ($graph_heigth + $graph_offset_y)-1, $black);
imagerectangle ($im, 0, 0, $width-1, $heigth-1, $black);

$result = pg_query ($conn, "select EXTRACT(EPOCH FROM TIMESTAMP	 '".date ("Y-m-d", mktime(0,0,0,$month,$day,$year)) ." 00:00:00');");
$time_start = pg_fetch_result($result, 0,0);
$result = pg_query ($conn, "select EXTRACT(EPOCH FROM TIMESTAMP '".date ("Y-m-d", mktime(0,0,0,$month,$day,$year)) ." 23:59:59');");
$time_end   = pg_fetch_result($result, 0,0);

$period = $time_end - $time_start;
$result = pg_query ($conn, "select counter, EXTRACT(EPOCH FROM date_of_record ) from bw_data
				where cast(date_of_record as date) = '".
				date ("Y-m-d", mktime(0,0,0,$month,$day,$year)) ."' and uid = $uid 
				order by date_of_record;");
		if (!$result) {
			echo "An error occured.\n";
			exit;
		}
		
$max_bandwidth=0;
$last_timestamp=$time_start;
for ($i = 0; $i < pg_num_rows($result); $i++)
{
	$bandwidth = pg_fetch_result($result,$i,0);
	if ($bandwidth > $max_bandwidth){
		$max_bandwidth = $bandwidth;
		$max_bandwidth_time = pg_fetch_result($result,$i,1) - $last_timestamp;
	}
	$last_timestamp = pg_fetch_result($result,$i,1);
}

for ($i=1; $i < 24 && $big_picture; $i++)
{
	// Hour lines
	imageline ($im, 
		($graph_width * ($i/24))  + $graph_offset_x,
		$graph_heigth + $graph_offset_y - ($graph_heigth / 5),
		($graph_width * ($i/24))  + $graph_offset_x,
		$graph_heigth - ($graph_heigth / 1.75) + $graph_offset_y,
		$black);
	if ($i > 9)
		$text_offset = -5;
	else
		$text_offset = -1;
	// Hour texts
	imagestring ($im, 2, ($graph_width * ($i/24))  + $graph_offset_x + $text_offset,
		$graph_heigth + $graph_offset_y ,  $i, $black);
}
// Draw the 12'o clock line
imageline ($im, ($graph_width/2) + $graph_offset_x, $graph_offset_y, ($graph_width/2) + $graph_offset_x, $graph_heigth - ($graph_heigth / 5) + $graph_offset_y, $red);


$result = pg_query ($conn, "select counter, EXTRACT(EPOCH FROM date_of_record ) from bw_data
				where cast(date_of_record as date) = '".
				date ("Y-m-d", mktime(0,0,0,$month,$day,$year)) ."' and uid = $uid 
				and \"in\" = 'f' order by date_of_record;");
		if (!$result) {
			echo "An error occured.\n";
			exit;
		}
$current_x = $graph_offset_x;
$current_y = $graph_heigth + $graph_offset_y;
$max_pix_no_update = 30 * $graph_width/(60 *24); // Thirty minutes with no update is this many pixels 

if($big_picture){
	imagestring ($im, 2, 8, $graph_offset_y - 8,  round(conv_kb($max_bandwidth/$max_bandwidth_time)) . " KB/S", $black);
	imagestring ($im, 2, 8, $graph_offset_y + ($graph_heigth/2) - 8,  round(conv_kb(($max_bandwidth/2)/$max_bandwidth_time)) . " KB/S", $black);
	imagestring ($im, 2, 8, $graph_offset_y + ($graph_heigth/4) - 8,  round(conv_kb((($max_bandwidth/4)/$max_bandwidth_time)*3)) . " KB/S", $black);
	imagestring ($im, 2, 8, $graph_offset_y + (($graph_heigth/4)*3) - 8,  round(conv_kb((($max_bandwidth/4)/$max_bandwidth_time))) . " KB/S", $black);
}
for ($i = 0; $i < pg_num_rows($result); $i++)
{
	$sample_time = pg_fetch_result($result,$i,1)."<br>";
	$bandwidth = pg_fetch_result($result,$i,0);
	$diff = $sample_time - $time_start;

	$pixel_x1 = (($diff/$period) * ($graph_width)) + $graph_offset_x;
	$pixel_y1 = ($graph_heigth + $graph_offset_y);
	$pixel_x2 = $pixel_x1;
	$pixel_y2 = $graph_heigth + $graph_offset_y - (($bandwidth/$max_bandwidth) * ($graph_heigth));
	// There has past some time since last update so bring the line to the bottom and over to the next update...
	if (($pixel_x1 - $current_x) > $max_pix_no_update){
		imageline ($im, 
		$current_x -1,
		$current_y -1,
		$current_x -1 ,
		($graph_heigth + $graph_offset_y) -1,
		IMG_COLOR_BRUSHED);
		$current_y = ($graph_heigth + $graph_offset_y);
		
		imageline ($im, 
		$current_x -1,
		$current_y -1,
		$pixel_x2 -1,
		($graph_heigth + $graph_offset_y) -1,
		IMG_COLOR_BRUSHED);
		$current_x = $pixel_x2;
	}
	
	imageline ($im, 
	$current_x-1,
	$current_y-1,
	$pixel_x2-1,
	$pixel_y2-1,
	IMG_COLOR_BRUSHED);
	$current_x = $pixel_x2;
	$current_y = $pixel_y2;

}

//copy of above, to get out bw aswell
$result = pg_query ($conn, "select counter, EXTRACT(EPOCH FROM date_of_record ) from bw_data
				where cast(date_of_record as date) = '".
				date ("Y-m-d", mktime(0,0,0,$month,$day,$year)) ."' and uid = $uid 
				and \"in\" = 't' order by date_of_record;");
		if (!$result) {
			echo "An error occured.\n";
			exit;
		}

$current_x = $graph_offset_x;
$current_y = $graph_heigth + $graph_offset_y;
//fill the brush image
imagefilledrectangle($brush, 0,0,1,1, $brush_colour2);
// Activate the brush for all linedrawing with colour IMG_COLOR_BRUSHED
imagesetbrush($im,$brush);

for ($i = 0; $i < pg_num_rows($result); $i++)
{
	$sample_time = pg_fetch_result($result,$i,1)."<br>";
	$bandwidth = pg_fetch_result($result,$i,0);
	$diff = $sample_time - $time_start;

	$pixel_x1 = (($diff/$period) * ($graph_width)) + $graph_offset_x;
	$pixel_y1 = ($graph_heigth + $graph_offset_y);
	$pixel_x2 = $pixel_x1;
	$pixel_y2 = $graph_heigth + $graph_offset_y - (($bandwidth/$max_bandwidth) * ($graph_heigth));
	// There has past some time since last update so bring the line to the bottom and over to the next update...
	if (($pixel_x1 - $current_x) > $max_pix_no_update){
		imageline ($im, 
		$current_x -1,
		$current_y -1,
		$current_x -1 ,
		($graph_heigth + $graph_offset_y) -1,
		IMG_COLOR_BRUSHED);
		$current_y = ($graph_heigth + $graph_offset_y);
		
		imageline ($im, 
		$current_x -1,
		$current_y -1,
		$pixel_x2 -1,
		($graph_heigth + $graph_offset_y) -1,
		IMG_COLOR_BRUSHED);
		$current_x = $pixel_x2;
	}
	
	imageline ($im, 
	$current_x-1,
	$current_y-1,
	$pixel_x2-1,
	$pixel_y2-1,
	IMG_COLOR_BRUSHED);
	$current_x = $pixel_x2;
	$current_y = $pixel_y2;

}



imagepng ($im);
imagedestroy($im);
imagedestroy($brush);
?>
Return current item: phpBandwidth