I adapted a PHP script that presents a vertical bargraph showing a goal and
current value and percentage. It was from:
http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoa l-thermometer-like-bar-graph-with-php/
My adaption includes a text file for the goal and current value as well as
height and width. This "goalgraph.php" works OK:
<?php
$datafile = "goalgraphdata.txt";
if( !file_exists($datafile) ) {
$df = fopen( $datafile, "w" ) or die( "File Error" );
// cur, goal, height, width
fwrite( $df, "100\r\n50\r\n200\r\n25\r\n" );
fclose( $df );
}
if (isset($_GET['cur']) && isset($_GET['goal']) && isset($_GET['height']) &&
isset($_GET['width']) ) {
header("Content-type: image/png");
//setting the values we'll use in our script pulled in from GET
$height = trim($_GET['height']);
$width = trim($_GET['width']);
$goal = trim($_GET['goal']);
$current = trim($_GET['cur']);
$df = fopen( $datafile, "w" ) or die( "File Error" );
// cur, goal, height, width
fwrite( $df, "$current\r\n$goal\r\n$height\r\n$width\r\n" );
fclose( $df );
}
$df = fopen( $datafile, "r" ) or die( "File Error" );
$current = trim(fgets( $df ));
$goal = trim(fgets( $df ));
$height = trim(fgets( $df ));
$width = trim(fgets( $df ));
fclose( $df );
if ($current > $goal) {
$current = 100;
$percent= "100%!";
} else if ($current == "" || $current == "0") {
$percent = "0%";
} else {
$percent = round(($current/$goal)*100) . "%";
}
$im = @imagecreate($width,$height)
or die("Cannot Initialize new GD image stream");
$bg = ImageColorAllocate($im,200,200,200); // grey
$fg = ImageColorAllocate($im,255,0,0); //red
$text_color = imagecolorallocate($im, 0, 0, 0); //black
$text_color1 = imagecolorallocate($im, 255, 255, 0255); //white
$fill = $height-(($current/$goal)*$height);
imagefilledrectangle($im, 0, $fill, $width, $height, $fg);
$font = 3;
imagestring($im, $font, 2, 0, trim($goal), $text_color);
imagestring($im, 5, 5, $height-$percent*$height/200-8, $percent,
$text_color1);
imagestring($im, $font, 2, $height-$percent*$height/100,
trim($current), $text_color);
imagepng($im);
imagedestroy($im);
?>
I am able to access this script using a simple HTML file if I do it with a
direct <img src="goalgraph.php" />. But when I try to do so using PHP within
the HTML, using echo, it does not work. I tried renaming the file php and
adding the "AddType application/x-httpd-php .htm" to a .htaccess file, but
no joy. Here is what I have:
<html>
<body>
Goalgraph
<hr />
<?php
//echo '<img src="' . "goalgraph.php" . '" />' . "\n";
$datafile = "goalgraphdata.txt";
if( file_exists($datafile) ) {
echo "File Found\n";
$df = fopen( $datafile, "r" ) or die( "File Error" );
$current = trim(fgets( $df ));
$goal = trim(fgets( $df ));
$height = trim(fgets( $df ));
$width = trim(fgets( $df ));
fclose( $df );
echo '<img src="' . htmlspecialchars("goalgraph.php?cur=" .
urlencode($current) . "&goal=" . urlencode($goal) .
"&width=" . urlencode($width) . "&height=" . urlencode($height) ) . '"
alt="Help us achieve our goal"' . " /> \n";
}
else
echo "File not found\n";
?>
<hr />
<img src="goalgraph.php" />
</body>
</html>
It's been a long time since I have done anything with PHP or HTML for that
matter, so I'm quite rusty, and it may be something simple. Any help will be
most kindly appreciated.
Thanks!
Paul
|