purefan
11-01-2005, 11:57 PM
greetings!!
I am working in a page to display an image created based on information from a database.
The idea is to show the last 6 tournaments played placing MyDots (a small function I wrote to draw a few dots altogether) and connecting them with lines (green if improving, red if not). So far these features are working ok but I am trying to place a scale bar on the left of the image indicating the actual percentage acquired in each tournament and the values returned from the database are too close to each other, i.e. in Tourn1 the user got 78% and in Tourn2 80%, when displaying the 78 and 80 these would overlap and I dont know how to fix this.
The database has the following fields:
Date -> 8digit integer since im exclusively using it for sorting purposes
Name -> 25 varchar
Percentage -> 3 digit int (100 at max)
here is the code, i've tried to document it properly:
<?php
session_start();
// create the image
$image = imagecreate(600, 300);
//declare some colors
$bgColor = imagecolorallocate ($image, 0, 0, 0);
$Green = imagecolorallocate ($image, 0, 255, 0);
$Red = imagecolorallocate ($image, 255, 0, 0);
$textColor = imagecolorallocate ($image, 255, 255, 255);
$Gray = imagecolorallocate ($image, 100, 100, 100);
//My function, just draws a few extra dots
function Mydot($imagen,$x, $y)
{
$_color = imagecolorallocate ($imagen, 255, 255, 255);
imagesetpixel( $imagen, $x, $y, $_color );
imagesetpixel( $imagen, $x+1, $y, $_color );
imagesetpixel( $imagen, $x-1, $y, $_color );
imagesetpixel( $imagen, $x, $y+1, $_color );
imagesetpixel( $imagen, $x, $y-1, $_color );
imagesetpixel( $imagen, $x, $y+2, $_color );
imagesetpixel( $imagen, $x, $y-2, $_color );
imagesetpixel( $imagen, $x+2, $y, $_color );
imagesetpixel( $imagen, $x-2, $y, $_color );
imagesetpixel( $imagen, $x-2, $y+2, $_color );
imagesetpixel( $imagen, $x-2, $y-2, $_color );
imagesetpixel( $imagen, $x+2, $y+2, $_color );
imagesetpixel( $imagen, $x+2, $y-2, $_color );
}//MyDot
////////////////////////// Selecting the Database and open a connection /////////////////////
$dbhost = "TheRealValue";
$dbusuario = "TheRealValue";
$dbpassword = "TheRealValue";
//connecting to the database
$conn = mysql_connect($dbhost, $dbusuario, $dbpassword) or die ('Error connecting to mysql');
$dbname = "test";
mysql_select_db($dbname);
/////////////////////////////////////////////////////////////////////////////////////////////
//trying to sort the Table to set it ready for output
$orderna = "ALTER TABLE ChessTournaments ORDER BY Date ASC";
mysql_query($orderna);
$query = "SELECT Date, Name, Percentage FROM ChessTournaments";
$result = mysql_query($query);
/*Declare arrays for storing the information from database, Easier than outputting all at once I think*/
$percents = array();
$names = array();
//variable to keep a record as for where the pointer is set to
$Count = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
//Next if since only want to display 6 tournaments
if($Count<6){
$percents[$Count] = $row["Percentage"];
$names[$Count] = $row["Name"];
//trying ti increase the percentages margin...doesnt work
$percent[$Count] = ($percent[$Count] * 2)/3;
$Count=$Count+1;
}//counting
}//mysql fetch
// Closing connection
mysql_close($conn);
//$equis holds the X positions for displaying the MyDots and names
$equis = array(150,200,250,300,350,400);
//reseting....
$Count = 0;
foreach($percents as $valor=>$value)
{
/*draw to $image the percentages to the left of the image. 150-$percents[$valor] goes 100 pixels from top and then back percents[valor]
which results in the bottom-to-top proper display of the number*/
imagestring($image,2,8,150-$percents[$valor],$percents[$valor],$textColor);
//print MyDots where a new tourn has taken place
Mydot($image,$equis[$Count],150-$value);
//IF its within range
if(($percents[$valor])>0 && ($percents[$valor])<101){
//choose proper color
if($percents[$valor+1]>$percents[$valor])
{
$Use_color=$Red;
}//choose Red
else {$Use_color=$Green;}//choose Green
//Print the line connecting the dots
imageline($image,$equis[$Count],150-$percents[$valor],$equis[$Count+1],150-$percents[$valor+1],$Use_color);
//Print the name of the tournament
imagestringup($image,3,$equis[$Count]-15,280,$names[$Count],$textColor);
//Print the line from the name to the dot
imageline($image,$equis[$Count],280,$equis[$Count],150-$percents[$valor],$Gray);
}//if siguiente existe
$Count=$Count+1;
}
// send several headers to make sure the image is not cached
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');
// send the image to the browser
imagejpeg($image);
// destroy the image to free up the memory
imagedestroy($image);
?>
so summarizing: I need the percentages to widen up their margin so they dont overlap on the left. Right now they are 1 pixel away from each other but I need them something at least 8 pixels away.
Also if you can suggest any improvement on my code here it would be great!.
my latest 'version' is found here:
http://purefan.xennos.com/ajedrez/torneos.php
I am working in a page to display an image created based on information from a database.
The idea is to show the last 6 tournaments played placing MyDots (a small function I wrote to draw a few dots altogether) and connecting them with lines (green if improving, red if not). So far these features are working ok but I am trying to place a scale bar on the left of the image indicating the actual percentage acquired in each tournament and the values returned from the database are too close to each other, i.e. in Tourn1 the user got 78% and in Tourn2 80%, when displaying the 78 and 80 these would overlap and I dont know how to fix this.
The database has the following fields:
Date -> 8digit integer since im exclusively using it for sorting purposes
Name -> 25 varchar
Percentage -> 3 digit int (100 at max)
here is the code, i've tried to document it properly:
<?php
session_start();
// create the image
$image = imagecreate(600, 300);
//declare some colors
$bgColor = imagecolorallocate ($image, 0, 0, 0);
$Green = imagecolorallocate ($image, 0, 255, 0);
$Red = imagecolorallocate ($image, 255, 0, 0);
$textColor = imagecolorallocate ($image, 255, 255, 255);
$Gray = imagecolorallocate ($image, 100, 100, 100);
//My function, just draws a few extra dots
function Mydot($imagen,$x, $y)
{
$_color = imagecolorallocate ($imagen, 255, 255, 255);
imagesetpixel( $imagen, $x, $y, $_color );
imagesetpixel( $imagen, $x+1, $y, $_color );
imagesetpixel( $imagen, $x-1, $y, $_color );
imagesetpixel( $imagen, $x, $y+1, $_color );
imagesetpixel( $imagen, $x, $y-1, $_color );
imagesetpixel( $imagen, $x, $y+2, $_color );
imagesetpixel( $imagen, $x, $y-2, $_color );
imagesetpixel( $imagen, $x+2, $y, $_color );
imagesetpixel( $imagen, $x-2, $y, $_color );
imagesetpixel( $imagen, $x-2, $y+2, $_color );
imagesetpixel( $imagen, $x-2, $y-2, $_color );
imagesetpixel( $imagen, $x+2, $y+2, $_color );
imagesetpixel( $imagen, $x+2, $y-2, $_color );
}//MyDot
////////////////////////// Selecting the Database and open a connection /////////////////////
$dbhost = "TheRealValue";
$dbusuario = "TheRealValue";
$dbpassword = "TheRealValue";
//connecting to the database
$conn = mysql_connect($dbhost, $dbusuario, $dbpassword) or die ('Error connecting to mysql');
$dbname = "test";
mysql_select_db($dbname);
/////////////////////////////////////////////////////////////////////////////////////////////
//trying to sort the Table to set it ready for output
$orderna = "ALTER TABLE ChessTournaments ORDER BY Date ASC";
mysql_query($orderna);
$query = "SELECT Date, Name, Percentage FROM ChessTournaments";
$result = mysql_query($query);
/*Declare arrays for storing the information from database, Easier than outputting all at once I think*/
$percents = array();
$names = array();
//variable to keep a record as for where the pointer is set to
$Count = 0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
//Next if since only want to display 6 tournaments
if($Count<6){
$percents[$Count] = $row["Percentage"];
$names[$Count] = $row["Name"];
//trying ti increase the percentages margin...doesnt work
$percent[$Count] = ($percent[$Count] * 2)/3;
$Count=$Count+1;
}//counting
}//mysql fetch
// Closing connection
mysql_close($conn);
//$equis holds the X positions for displaying the MyDots and names
$equis = array(150,200,250,300,350,400);
//reseting....
$Count = 0;
foreach($percents as $valor=>$value)
{
/*draw to $image the percentages to the left of the image. 150-$percents[$valor] goes 100 pixels from top and then back percents[valor]
which results in the bottom-to-top proper display of the number*/
imagestring($image,2,8,150-$percents[$valor],$percents[$valor],$textColor);
//print MyDots where a new tourn has taken place
Mydot($image,$equis[$Count],150-$value);
//IF its within range
if(($percents[$valor])>0 && ($percents[$valor])<101){
//choose proper color
if($percents[$valor+1]>$percents[$valor])
{
$Use_color=$Red;
}//choose Red
else {$Use_color=$Green;}//choose Green
//Print the line connecting the dots
imageline($image,$equis[$Count],150-$percents[$valor],$equis[$Count+1],150-$percents[$valor+1],$Use_color);
//Print the name of the tournament
imagestringup($image,3,$equis[$Count]-15,280,$names[$Count],$textColor);
//Print the line from the name to the dot
imageline($image,$equis[$Count],280,$equis[$Count],150-$percents[$valor],$Gray);
}//if siguiente existe
$Count=$Count+1;
}
// send several headers to make sure the image is not cached
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');
// send the image to the browser
imagejpeg($image);
// destroy the image to free up the memory
imagedestroy($image);
?>
so summarizing: I need the percentages to widen up their margin so they dont overlap on the left. Right now they are 1 pixel away from each other but I need them something at least 8 pixels away.
Also if you can suggest any improvement on my code here it would be great!.
my latest 'version' is found here:
http://purefan.xennos.com/ajedrez/torneos.php