Page 1 of 1

Looping imagestrings

Posted: Wed Oct 18, 2006 2:59 pm
by jeeep
I have a script that will get player stats and display them. It works fine but the results are unorganized. So I display the player stats in descending order by creating an array:

Code: Select all

$Player1_kills = get_data ($fr, "Player1_kills = ");
 $Player1_deaths = get_data ($fr, "Player1_deaths = ");
 $Player1_KD =($Player1_kills / $Player1_deaths);
 $Player1_KD = round($Player1_KD, 3);

$Player2_kills = get_data ($fr, "Player2_kills = ");
 $Player2_deaths = get_data ($fr, "Player2_deaths = ");
 $Player2_KD =($Player2_kills / $Player2_deaths);
 $Player2_KD = round($Player2_KD, 3);

$Player3_kills = get_data ($fr, "Player3_kills = ");
 $Player3_deaths = get_data ($fr, "Player3_deaths = ");
 $Player3_KD =($Player3_kills / $Player3_deaths);
 $Player3_KD = round($Player3_KD, 3); 

$score = array(   
    "Player1" => "$player1_KD",   
   "Player2" => "$player2_KD",   
   "Player3" => "$player3_KD",   
    
  );
  
   // Sort the array in reverse order with its elements
 arsort($score);
  
   // Print 3 top elements.
   for ($i=0; $i<3; $i++) {   if (
   $row = each($score)) {     echo
 "$row[key] $row[value]"
  ;   }
 }
The output of this:

Code: Select all

Player3 1.786  Player1 1.327   Player2 0.88
What I would like to do is to create a loop that outputs imagestring since I am making a png image.
Something similar to this:

Code: Select all

header("Content-type: image/png");
    $bg = "statsig1.png";
    $im = @ImageCreateFromPNG($bg);
    $width =1;
    $font  = 2;
    $fonta  = 3;
    $white = imagecolorallocate($im, 255, 255, 255); 
    $red = imagecolorallocate($im, 255, 0, 0);
    $black =  imagecolorallocate($im, 0, 0, 0);


imagestring($im, $font, 24, 303, "Player1", $white);
imagestring($im, $font, 149, 303, $Player1_kills, $white);
imagestring($im, $font, 251, 303, $Player1_deaths, $white);
imagestring($im, $font, 350, 303, $Player1_KD, $red);

imagestring($im, $font, 24, 320, "Player2", $white);
imagestring($im, $font, 149, 320, $Player2_kills, $white);
imagestring($im, $font, 251, 320, $Player2_deaths, $white);
imagestring($im, $font, 350, 320, $Player2_KD, $red);

imagestring($im, $font, 24, 337, "Player3", $white);
imagestring($im, $font, 149, 337, $Player3_kills, $white);
imagestring($im, $font, 251, 337, $Player3_deaths, $white);
imagestring($im, $font, 350, 337, $Player3_KD, $red);
But the loop has to be sorted by the players_kd and must also contaion the players name, player deaths and player kills. Can anyone give me any insight?

Posted: Wed Oct 18, 2006 3:05 pm
by feyd
It would seem that your imagestring() groups could be built as three calls to a (user created) function. The coordinates are a relative to the first, so you pass the first coordinate set into it, the image to write to and the strings to write.

Posted: Wed Oct 18, 2006 5:13 pm
by jeeep
yikes! could you give me an example bud?

Posted: Wed Oct 18, 2006 5:15 pm
by feyd
You've got almost everything you need in your snippet already. You just need a function and the proper calls.

Posted: Wed Oct 18, 2006 11:16 pm
by jeeep
Ok I understand what your saying, just have to figure this one out. Heres what I need to do: 1. Figure out how to create X and Y coordinates that are "relative" to the first, 2. Create an imagestring with the proper variables in it.

Posted: Thu Oct 19, 2006 2:02 am
by jeeep
Here is what I've got so far, but I get the error that says "cant modify headers":

Code: Select all

$jeeep_kills = get_data ($fr, "jeeep_kills = ");
 $jeeep_deaths = get_data ($fr, "jeeep_deaths = ");
 $jeeep_KD = ($jeeep_kills / $jeeep_deaths);
 $jeeep_KD = round($jeeep_KD, 3);
  
 $crash_kills = get_data ($fr, "crash_kills = ");
 $crash_deaths = get_data ($fr, "crash_deaths = ");
 $crash_KD = ($crash_kills / $crash_deaths);
 $crash_KD = round($crash_KD, 3);

 $kiwi_kills = get_data ($fr, "kiwi_kills = ");
 $kiwi_deaths = get_data ($fr, "kiwi_deaths = ");
 $kiwi_KD =($kiwi_kills / $kiwi_deaths);
 $kiwi_KD = round($kiwi_KD, 3);

 


$total_KD = ((($jeeep_kills / $jeeep_deaths) + ($crash_kills / $crash_deaths) + ($kiwi_kills / $kiwi_deaths))/3);
$total_KD = round($total_KD, 3);



    // Fill an array with top score data.
  $score = array(   
    "Jeeep.eX $jeeep_kills $jeeep_deaths" => "$jeeep_KD ",   
   "Kiwi.eX $kiwi_kills  $kiwi_deaths" => " $kiwi_KD",   
   "Crash.eX $crash_kills  $crash_deaths" => "$crash_KD",   
    
  );

  header("Content-type: image/png");
    $bg = "makesig1.png";
    $im = @ImageCreateFromPNG($bg);
    $width =1;
    $font  = 2;
    $fonta  = 3;
    $white = imagecolorallocate($im, 255, 255, 255); 
    $red = imagecolorallocate($im, 255, 0, 0);
    $black =  imagecolorallocate($im, 0, 0, 0);  




 // Sort the array in reverse order with its elements
 arsort($score);


               $x = 24   ;
  


  
   for($i = 0; $i <600; $i++)
{
$line = imagestring($im, $font, $x, "$y = $i." , "$row[key]   $row[value]", $white);
   }
 
 for ($i=0; $i<100; $i++) {  if (
   $row = each($score)) {     print(
 " $line ")
  ;   }
 }
  
$path = 'makesig99199.png';


imagepng($im, $path);



readfile($path );
      ?>
I'm sure there is more wrong with this script but I think there are two main problems with it. 1) The createimagefrompng and the "print" I think are confliciting. 2) The loop

Code: Select all

for($i = 0; $i <600; $i++)
{
$line = imagestring($im, $font, $x, "$y = $i." , "$row[key]   $row[value]", $white);
   }
is "$y =$i." appropriate?

Posted: Thu Oct 19, 2006 6:51 am
by feyd
viewtopic.php?t=1157 should be of interest.

Posted: Thu Oct 19, 2006 9:59 am
by jeeep
So I re-wrote the bottom portion of the above script and now the imagestring produces the proper text but its all on top of each other:

Code: Select all

arsort($score);


$x = 24   ;  
for($i = 0; $i <count($score); $i++)
{
$y =  $i + 16 ;
   }



 
 for ($i=0; $i<100; $i++) {  if (
   $row = each($score)) {     imagestring($im, $font, $x, $y  , "$row[key]   $row[value]", $white);  }
 } 

 $path = 'makesig99199.png';


imagepng($im, $path);



readfile($path );

      ?>
which tells me the loop for $y is not working, any help?

Posted: Thu Oct 19, 2006 10:05 am
by feyd
The x coordinate isn't varying, but your original's did.

Posted: Thu Oct 19, 2006 10:23 am
by jeeep
Dont need the X coordinate doesnt need to vary. The imagestrings should only be going down the Y axis. But with the current code the imagestring isnt moving down the Y axis. What am I doing wrong?

Posted: Thu Oct 19, 2006 10:42 am
by feyd
Well, you have a for loop that just manipulates the y coordinate, and you have a separate for loop that calls imagestring().

Posted: Thu Oct 19, 2006 10:54 am
by jeeep
I finally figured it out but thanks for your advise

Posted: Thu Oct 19, 2006 12:52 pm
by jeeep
Is there anyway to align this?

Code: Select all

$score = array(   
    "Jeeep.eX $jeeep_kills $jeeep_deaths" => "$jeeep_KD ",   
   "Kiwi.eX $kiwi_kills  $kiwi_deaths" => " $kiwi_KD",   
   "Crash.eX $crash_kills  $crash_deaths" => "$crash_KD",   
   
  );
The output is kind of messy. Since this comes out in an imagestring, I would suppose that if it is in order in the array it will be in order then. I know if I put ten spaces inbetween Jeeep.eX and $jeeep_kills there will be ten spaces between them on the image.

Posted: Thu Oct 19, 2006 2:17 pm
by feyd
sprintf() may be of interest.