You are not logged in.

#1 07 Nov 2006 10:24 am

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Dynamic images in php

So does anyone have any simple code examples of creating images in php.  Basically i'm looking at recreating the graphs like BF2142 has in game.

Possibly after that, creating sigs dynamically.

Offline

 

#2 07 Nov 2006 10:46 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

you can either use GD/GD2 (which is usually built in but leaves a lot to be desired, but is functional) or image magick (I've never used this one, but hear its very good).

Code:

$img = imagecreatefrompng($path_to_image);

will give you a png image to draw on.


Code:

$img = imagecreatetruecolor($width, $height);

will create a blank image for you to draw on.

in either of the examples, you then draw on top of the image.

Code:

imagettftext($img, $font_size, $angle, $x, $y, $txt_color, $path_to_font, $string_to_draw);

will draw a string at $x,$y and at angle $angle with the given color and font (font path is like: "/fonts/ARIALBD.TTF" which is the local path to the font file you have uploaded to the server).

things like imagecopymerge and imagecopyresampled will let you draw another image on top of the original image

then you can draw shapes, fill shapes or whatever.  here's the api docs for that sort of thing: http://us2.php.net/manual/en/function.gd-info.php (functions are on the left hand side).

for things like charts (uhg, let me tell you drawing charts is a pain), I'd suggest using a charting library like JpGraph or something.


php.net's api docs have a lot of examples.

Offline

 

#3 07 Nov 2006 10:59 am

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

I found an example of an XY Scatter which is what I was planning on using smile

http://www.velox-mortis.com/images/xygraph.php

I'll have to clean it up, add gridlines and connecting dots but it gives me the basics.  I have an inhouse graphics package in C# that some guys at my work have been working on I can probably pick their brains for some good ideas on how to format the graph.

edit: source for the file is from a comment at
http://us2.php.net/manual/en/function.imagecreate.php

i added a rand()%x for the source data instead of always having x^2.

Last edited by Craigins (07 Nov 2006 11:15 am)

Offline

 

#4 07 Nov 2006 11:09 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

GDI makes GD2 look like crap.  it doesnt have some of the nicer features available in GDI+  I've had to write graphing libraries (in both php and .net) and both were more trouble than they were worth.  the C# one (which I still have somewhere) was a pain, because unlike a php graph that just generates an image this had to scale from like 2px x 2px to full screen across 2 monitors (a lot more calculations involved).

if you're doing it for the web, there's some pretty slick things in flex that you could probably use (which would look almost exactly like the in game UI).

Offline

 

#5 07 Nov 2006 11:34 am

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

I added some lines to my xy graph, doesn't seem too hard.

I'm not really a designer by any means, i'm just a programmer so making things look good isn't my strong point lol.

Offline

 

#6 07 Nov 2006 11:35 am

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Dynamic images in php

If you ever need any graphics stuff done, let me know dude.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#7 07 Nov 2006 11:45 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

give JpGraph a look.  they have tons of examples and they have really awesome looking charts (and its free).

Offline

 

#8 07 Nov 2006 12:24 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Dynamic images in php

I am free too tongue, and I will do partially legal graphics with partially legal programs smile


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#9 07 Nov 2006 2:02 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

i'll take a look at jpGraph when i get home.  Winzip doesn't like the archive file(tar.gz) and thats all i have at work smile

Offline

 

#10 07 Nov 2006 7:36 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

OMG IE7 sucks for ftping to my website.  after 2 hours of fighting with it to get it to open my site its going to take an hour to upload jpgraph

Offline

 

#11 08 Nov 2006 6:24 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

I'd never attempt to ftp using ie.  I've used filezilla for as long as I can remember. http://sourceforge.net/projects/filezilla/

Offline

 

#12 09 Nov 2006 7:32 am

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

Actually the IE ftp ended up timing out over and over again so I just used it to create the directories, and then i used the command line ftp to upload the files.  Sucks though because i did ascii mode for the html files, so none of the pictures ended up uploading properly.

Offline

 

#13 09 Nov 2006 8:54 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

http://www.velox-mortis.com/stats/showgraph.html

using this data:

1161129600    1.01
1161216000    0.94
1161302400    1.14
1161388800    1.11
1161475200    1.03
1161561600    0.99
1161648000    0.93
1161734400    0.92
1161820800    0.94
1161907200    0.97
1161993600    1.03
1162080000    1.08
1162166400    1.09
1162252800    1.10
1162339200    1.09
1162425600    1.09
1162512000    1.11
1162598400    1.17
1162684800    1.21
1162771200    1.21

shows my score per minute!

Does anyone have any examples of text parsing, without going into regular expressions?

Like an example of how to parse

Code:

Port: 2436
Page: getplayerprogress.aspx?mode=spm&scale=game&auth=Uj[XMjBhLt0EtyjVyPW[xw__
<binary data that won't copy/paste>
Date: Thu, 09 Nov 2006 02:50:59 GMT
Server: Microsoft-IIS/6.0
cluster-server: bf2142web1
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 414

O
H    pid    asof
D    81299177    1163040659
H    date    spm
D    1161129600    1.01
D    1161216000    0.94
D    1161302400    1.14
D    1161388800    1.11
D    1161475200    1.03
D    1161561600    0.99
D    1161648000    0.93
D    1161734400    0.92
D    1161820800    0.94
D    1161907200    0.97
D    1161993600    1.03
D    1162080000    1.08
D    1162166400    1.09
D    1162252800    1.10
D    1162339200    1.09
D    1162425600    1.09
D    1162512000    1.11
D    1162598400    1.17
D    1162684800    1.21
D    1162771200    1.21
$    336    $

I'd probably split on Content-Length: then on $ then again on \n after that loop through till i find the first d take that as my key, then the second d starts the data.

Last edited by Craigins (09 Nov 2006 9:06 pm)

Offline

 

#14 09 Nov 2006 10:59 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

this is my code that wraps up the result string into an associative array that I use to generate my sig here for bf2

Code:

    function _get_result_set($str) {
        $lines = explode("\n", $str);
        $results = array();
        $line_count = count($lines);
        $line_num = 0;
        $stats = array();
        while ($line_num < $line_count) {
            $next_line = $line_num + 1;
            $line_one = explode("\t", $lines[$line_num]);
            $len = count($line_one);
            if($line_one[0] != '$') {
                if($line_one[0] == 'O') {
                    $line_num++;
                    continue;
                }
                else if($line_one[0] == 'H') {
                    $line_two = explode('    ', $lines[$next_line]);
                    $line_two_len = count($line_two);
                    if($line_two_len > 0 && $line_two[0] == 'D') {                
                        for($index = 1; $index <= $len; $index++) {
                            if(isset($line_one[$index])) {
                                $results[$line_one[$index]] = trim($line_two[$index]);
                            }
                        }
                    }else {
                        $line_num++;
                        continue;
                    }
                }
                $line_num+=2;
            }else {
                break;
            }
        }
        return $results;
    }

this is to parse a result like:

H head1 header2
D data1 data2

but could easily be adapted to include multiple data rows.

Offline

 

#15 10 Nov 2006 5:46 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

http://www.velox-mortis.com/stats/showgraph.html

got my graph process up and running.  enter in a tabbed delimited list(like from excel) first column being the unix time stamp and it will automatically graph it.  doesn't matter how many columns there are(just has to be more than 1 column.

Offline

 

#16 11 Nov 2006 8:51 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

slowly but surely.  I have my packet monitoring program writing sql statements to populate my mysql database, right now i have to run them manually.  But i have my graphing page working now for role and score.  its dynamic so its pretty nice, just have to plug in the names of the columns depending on the mode.

http://www.velox-mortis.com/stats/viewp … ;mode=role
http://www.velox-mortis.com/stats/viewp … mode=score


edit:
http://www.velox-mortis.com/stats/craigins.html

example of all the ones i have coded so far.

Last edited by Craigins (11 Nov 2006 9:04 pm)

Offline

 

#17 11 Nov 2006 9:40 pm

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

those look awesome!

so how often are you pulling stats?  Ive thought about putting one of my other servers I have up pulling stats, but haven't had any time to get it working.

Offline

 

#18 11 Nov 2006 11:13 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

I set up all the progress pages.

Right now i'm only using the packet filtering program to pull stats for my guy.  I just start it, go into BF2142, look at all the progress pages and then stop the program.  It writes out a file with all the sql inserts that I need, then I just copy them and paste them into a form I have set up and it inserts the new data, if it is new, or skips it if it is already in the database.

http://www.velox-mortis.com/stats/craigins.html

Tomorrow I plan on working on getplayerinfo.aspx   then hopefully awards and unlocks.

FYI heres the code I use for the graph page, pretty straightforward:
example of one of the includes:

Code:

<?php
$col=array(array());
$col[0][0]="date";
$col[0][1]="Date";
$col[1][0]="hls";
$col[1][1]="Heals";
$col[2][0]="rps";
$col[2][1]="Repairs";
$col[3][0]="rvs";
$col[3][1]="Revives";
$col[4][0]="resp";
$col[4][1]="Resupply";
$col[5][0]="Support Role Points";
$col[5][1]="Points";
?>

Code:

<?php
include ("src/jpgraph.php");
include ("src/jpgraph_log.php");
include ("src/jpgraph_line.php");

$colors = array("blue","orange","red","green","yellow","purple");

    $pid = $_GET["pid"];
    $mode = $_GET["mode"];
    if($mode == "role")
    {
        include "roledef.php";
    }
    else if($mode == "score")
    {
        include "scoredef.php";
    }
    else if($mode == "flag")
    {
        include "flagdef.php";
    }
    else if($mode=="kills")
    {
        include "killsdef.php";
    }
    else if($mode=="spm")
    {
        include "spmdef.php";
    }
    else if($mode=="sup")
    {
        include "supdef.php";
    }
    else if($mode=="ttp")
    {
        include "ttpdef.php";
    }
    else if($mode=="twsc")
    {
        include "twscdef.php";
    }
    else if($mode=="waccu")
    {
        include "waccudef.php";
    }
    else if($mode=="wl")
    {
        include "wldef.php";
    }


    $link = mysql_connect('localhost', '', '')
                            or die('');
    mysql_select_db('veloxmor_phpbb2') or die('');
    $query = "select * from bf2142_progress_$mode where pid=$pid";
    //echo "$query";

    //Query phpbb to verify login info and get class/level info.
    $result = mysql_query($query) or die('<!--Query failed: ' . mysql_error() . '-->');
    //echo "<br>Done Querying";
    //echo "<br>reading results";
    $ydata = array(array());
    $xdata = array();
    $i=0;
    while($line = mysql_fetch_array($result, MYSQL_ASSOC))
    {    
        $xdata[$i]=date("m.d.Y",$line[$col[0][0]]);
        for($j=1;$j<count($col)-1;$j++)
        {
            $ydata[$j-1][$i] = $line[$col[$j][0]];
        }
        $i++;
    }
    

$graph = new Graph(800,600,"auto");
$graph->SetScale("textlin");

$graph->img->SetMargin(80,140,20,100);
$graph->SetShadow();

$graph->ygrid->Show(true,true);
$graph->xgrid->Show(true,false);



$lineplots = array();
for($i=0;$i<count($ydata);$i++)
{
    // Create the linear plot
    $lineplots[$i]=new LinePlot($ydata[$i]);
    $lineplots[$i]->SetColor($colors[$i % count($colors)]);
    $lineplots[$i]->SetWeight(2);
    $lineplots[$i]->SetLegend($col[$i+1][1]);
    // Add the plot to the graph
    $graph->Add($lineplots[$i]);
}
$i++;


$graph->title->Set("Progress - ". $col[$i][0]);
$graph->title->SetFont(FF_FONT1,FS_BOLD);

// Specify the tick labels
$graph->xaxis->SetTickLabels($xdata);
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->title->set($col[0][1],"center");
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->SetTitlemargin(60);

$graph->yaxis->title->Set($col[$i][1]);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->SetLabelMargin(5);
$graph->yaxis->SetTitlemargin(50);
//$graph->yaxis->SetLabelAngle(30);
//$graph->yaxis->SetColor("blue");

$graph->legend->Pos(0.02,0.5,"right","center");

// Display the graph
$graph->Stroke();


    
?>

And some of the inserts i use:

Code:

INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161129600,1,0,0,16)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161216000,1,0,0,16)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161302400,1,0,0,16)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161388800,6,13,12,17)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161475200,17,13,30,33)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161561600,26,13,39,48)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161648000,26,19,39,49)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161734400,30,19,43,49)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161820800,35,19,50,53)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161907200,37,19,50,69)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1161993600,42,19,57,82)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162080000,76,19,74,82)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162166400,76,21,74,82)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162252800,82,31,77,90)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162339200,82,33,79,90)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162425600,82,33,79,90)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162512000,82,47,79,90)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162598400,82,51,80,205)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162684800,83,51,80,348)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1162771200,83,51,80,368)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1163116800,83,51,80,370)
INSERT INTO bf2142_progress_sup  (autots,pid,date,hls,rps,rvs,resp) VALUES (CURRENT_TIMESTAMP,81299177,1163203200,83,51,80,372)

and the C# used to parse the packet data:

Code:

private static string writeSqlFor(string page, System.Collections.ArrayList parms, string data)
        {
            string ret = "";
            string mode="",scale="",pid="", insertinto="";
            string[] fields;
            switch (page)
            {
                case "getplayerprogress.aspx":
                    foreach(string[] arg in parms)
                    {
                        switch (arg[0])
                        {
                            case "mode":
                                mode = arg[1];
                                break;
                            case "scale":
                                scale = arg[1];
                                break;
                            default:
                                break;
                        }
                    }
                    insertinto = String.Format("INSERT INTO bf2142_{0}_{1} ", page.Replace(".aspx","").Replace("getplayer",""), mode);
                    string[] lfsplit = data.Split('\n');
                    int i=0;
                    foreach (string ln in lfsplit)
                    {
                        i++;
                        if(ln.StartsWith("Content-Length: "))break;
                    }
                    bool header = false;
                    for (; i < lfsplit.Length; i++)
                    {
                        if (lfsplit[i].StartsWith("H\t"))
                        {
                            if (lfsplit[i].Equals("H    pid    asof"))
                            {
                                header = true;
                            }
                            else
                            {
                                fields = lfsplit[i].Substring(2).Split('\t');
                                insertinto += " (autots,pid," + lfsplit[i].Substring(2).Replace('\t', ',') + ") VALUES (CURRENT_TIMESTAMP," + pid + ",";
                                header = false;
                            }
                        }
                        else if (lfsplit[i].StartsWith("D\t"))
                        {
                            if (header)
                            {
                                pid = lfsplit[i].Substring(2, lfsplit[i].IndexOf('\t', 3)-2);
                            }
                            else
                            {
                                ret += insertinto + lfsplit[i].Substring(2).Replace('\t', ',') + ");\n";
                            }
                        }
                    }
                    break;
                case "getplayerinfo.aspx":
                    break;
                case "getleaderboard.aspx":
                    break;
                case "getbackendinfo.aspx":
                    break;
                default:
                    break;
            }
            return ret;
        }

I still have to figure out how to read the data that extends across multiple packets though.

Last edited by Craigins (11 Nov 2006 11:25 pm)

Offline

 

#19 13 Nov 2006 12:45 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

I didn't get that far with the get player info.  I can't do it exactly the same way as  player progress or I will end up with arrays that are over 100 elements long for 1 table(maps/vehicles/weapons).

if people are interested I should be able to pull out the site specific code(like the login validation, and database connection) that way people can write their own and just plug it in.  The login validation is used so that only certain people have access to post their stats to the site.

Last edited by Craigins (13 Nov 2006 12:52 pm)

Offline

 

#20 14 Nov 2006 7:14 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

thats pretty tight.  yea put something up if you get a chance.

Offline

 

#21 17 Nov 2006 1:15 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

stuff is dying down at work so i'm not mentally exhausted like i have been for the last 2 weeks.  Hopefully this weekend I can nail down the player info and backendinfo data, along with pulling out some of my site specific code.  Hopefully butcher could use some of the examples then for his site.

Offline

 

#22 17 Nov 2006 2:00 pm

Butcher
Moderator
From: Norway
Registered: Jul 2006
Posts: 308

Re: Dynamic images in php

I sure hope I can big_smile, just need the hot damned forum to stop pulling debug mode on me.


http://bamboocommandos.com/butcher_img/butchersig7.jpg

Offline

 

#23 19 Nov 2006 8:21 pm

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

I got the 2 hard pages for player info finished.

http://velox-mortis.com/stats/viewinfo. … p;mode=veh
http://velox-mortis.com/stats/viewinfo. … p;mode=map

Everything aside from weapons should be easy to do.  When I hit weapons I have to go monkey with the packet grabbing code so that I get the entire table data instead of just a partial header row.

Offline

 

#24 20 Nov 2006 7:56 am

MadHatter
Administrator
From: Dallas TX
Registered: Jun 2006
Posts: 529
Website

Re: Dynamic images in php

thats coming along nicely.  are you capturing all that info w/ winpcap?

Offline

 

#25 20 Nov 2006 10:42 am

Craigins
Extreme Member
From: Chicago, IL
Registered: Oct 2006
Posts: 58
Website

Re: Dynamic images in php

Yes i use that sharppcap project i posted earlier.  I monitor outgoing packets to see which pages I have requested, then when it comes back I do something(can't remember off the top of my head) and grab the page name with query string.  I then parse the query string to get all the options and pass it to my parsing function.  the function then parses the data depending on the page and the mode and spits out SQL statements for my mysql database on the web server.  Right now I have to manually go into the database and run the queries to populate the table.  Eventually i'm hoping that either my applicaiton will be able to directly connect to the mysql database, or that i can send the info through POSTing it to the submission form.

Offline

 



© 2003 - 2024 NullFX
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License