PDA

View Full Version : [PHP] SRL Stats Signatures



Frement
12-27-2010, 04:48 AM
So, the new SRL Stats system is out, and there is a nice API, why not make stats signatures?

Things you need:

A web-space with PHP & GD
RS Font (Any font will do, but rs.ttf is what I use). Download from HERE (http://www.frement.net/srl/rs.ttf)!
Some knownledge of PHP, alot is just given to you and explained, but basic understanding is good.


Lets start by having a look at the API at http://stats.villavu.com/api/script/2.


{
"last_commit_by:": "whiled",
"vars": [
[
12221,
"Iron"
]
],
"script": "Iron Miner",
"commits": 860,
"time": 14490,
"owner": "whisker",
"last_commit_on:": "Sun Dec 26 23:25:21 2010"
}

The syntax is JSON, and PHP has built-in parser for it.

Parse & Clean

Parsing the data from stats API into array:

<?php

$stats = file_get_contents("http://stats.villavu.com/api/script/2");
$stats_array = json_decode($stats, true);

?>
To see the structure of your $stats_array you can use function like print_r($stats_array); to see it.

Here is what print_r outputs:

Array
(
[last_commit_by:] => whiled
[vars] => Array
(
[0] => Array
(
[0] => 12221
[1] => Iron
)

)

[script] => Iron Miner
[commits] => 860
[time] => 14490
[owner] => whisker
[last_commit_on:] => Sun Dec 26 23:25:21 2010
)

Now we might want to put "vars" array into more easily accessible form. I made a simplify_vars() function for it.

<?php

$stats = file_get_contents("http://old.villavu.com/stats/api/script/2");
$stats_array = json_decode($stats, true);

function simplify_vars(&$stats_array) {
foreach ($stats_array["vars"] as $array) {
$stats_array[$array[1]] = $array[0];
}
unset($stats_array["vars"]);
}

simplify_vars($stats_array);

print_r($stats_array);

?>
Output:

Array
(
[last_commit_by:] => whiled
[script] => Iron Miner
[commits] => 860
[time] => 14490
[owner] => whisker
[last_commit_on:] => Sun Dec 26 23:25:21 2010
[Iron] => 12221
)
And as you can see, we can now access the Iron variable with $stats_array["Iron"].

Now that we have our array cleaned up, we can move to the fun part.

Display Stats

Setting font path:

putenv("GDFONTPATH=".realpath("."));

Creating image:

$img = imagecreatefrompng("sig_base_example.png");

I already have some colors ready that are used in RS, but you can allocate your own colors easily with:

imagecolorallocate($img, Red, Green, Blue);
Now that you have allocated your colors, you should have something like this:

<?php

$stats = file_get_contents("http://old.villavu.com/stats/api/script/2");
$stats_array = json_decode($stats, true);

function simplify_vars(&$stats_array) {
foreach ($stats_array["vars"] as $array) {
$stats_array[$array[1]] = $array[0];
}
unset($stats_array["vars"]);
}

simplify_vars($stats_array);

putenv("GDFONTPATH=".realpath("."));

$img = imagecreatefrompng("sig_base_example.png");

$color["yellow"] = imagecolorallocate( $img, 255, 255, 0 );
$color["white"] = imagecolorallocate( $img, 255, 255, 255 );
$color["green"] = imagecolorallocate( $img, 0, 255, 128 );
$color["orange"] = imagecolorallocate( $img, 254, 179, 0 );

?>
You might want to color your values differently, like RS does for item quantity, so here is two functions that you can use:

function rs_color($value) {
if ($value < 100000) {
return "yellow";
} elseif ($value > 100000 && $value < 10000000) {
return "white";
} else {
return "green";
}
}

function rs_value($value) {
if ($value < 100000) {
return $value;
} elseif ($value > 100000 && $value < 10000000) {
return substr($value, 0, strlen($value) - 3)."K";
} else {
return substr($value, 0, strlen($value) - 6)."M";
}
}

Drawing text on your image is easy, one function does it all:

imagettftext($img, 12, 0, 100, 80, $color["orange"], "rs.ttf", "IRON MINED: ");
imagettftext($img, 12, 0, 162, 80, $color[rs_color($stats_array["Iron"])], "rs.ttf", rs_value($stats_array["Iron"]));

Also you might want to have shadow on your text, so here is a function for that, it takes the same parameters as normal imagettftext():

function imagettftextshadow($image, $size, $angle, $x, $y, $color, $fontfile, $text) {
imagettftext($image, $size, $angle, $x + 1, $y + 1, imagecolorallocate($image, 0, 0, 0), $fontfile, $text);
imagettftext($image, $size, $angle, $x, $y, $color, $fontfile, $text);
}

When you have drawn all your texts, you are ready to show the image.

imagepng($img);
imagedestroy($img);

Now some final things, and you are ready. You need to specify headers for your content, which is image/png, so:

header("Content-type: image/png");

Now your script should look like this:

<?php

putenv("GDFONTPATH=".realpath("."));

header("Content-type: image/png");

function simplify_vars(&$stats_array) {
foreach ($stats_array["vars"] as $array) {
$stats_array[$array[1]] = $array[0];
}
unset($stats_array["vars"]);
}

function rs_color($value) {
if ($value < 100000) {
return "yellow";
} elseif ($value > 100000 && $value < 10000000) {
return "white";
} else {
return "green";
}
}

function rs_value($value) {
if ($value < 100000) {
return $value;
} elseif ($value > 100000 && $value < 10000000) {
return substr($value, 0, strlen($value) - 3)."K";
} else {
return substr($value, 0, strlen($value) - 6)."M";
}
}

function imagettftextshadow( $image, $size, $angle, $x, $y, $color, $fontfile, $text ) {
imagettftext( $image, $size, $angle, $x + 1, $y + 1, imagecolorallocate( $image, 0, 0, 0 ), $fontfile, $text );
imagettftext( $image, $size, $angle, $x, $y, $color, $fontfile, $text );
}

$stats = file_get_contents("http://old.villavu.com/stats/api/script/2");
$stats_array = json_decode($stats, true);

simplify_vars($stats_array);

$img = imagecreatefrompng("sig_base_example.png");

$color["yellow"] = imagecolorallocate( $img, 255, 255, 0 );
$color["white"] = imagecolorallocate( $img, 255, 255, 255 );
$color["green"] = imagecolorallocate( $img, 0, 255, 128 );
$color["orange"] = imagecolorallocate( $img, 254, 179, 0 );

imagettftextshadow($img, 12, 0, 100, 80, $color["orange"], "rs.ttf", "IRON MINED: ");
imagettftextshadow($img, 12, 0, 162, 80, $color[rs_color($stats_array["Iron"])], "rs.ttf", rs_value($stats_array["Iron"]));

imagepng($img);
imagedestroy($img);

?>

And it should output this:
http://www.frement.net/srl/stats/stats2.php

Helpful Resources


GD and Image Functions (http://www.php.net/manual/en/ref.image.php)
w3schools PHP Introduction (http://w3schools.com/php/php_intro.asp)

Capricorn
12-27-2010, 04:49 AM
Possible with any free hosts?

Frement
12-27-2010, 04:50 AM
Possible with any free hosts?

Almost every hosting company (even the ones that provide for free only) has PHP and GD installed.

EDIT: And I can host the files on my site also, if someone doesn't find any suitable free host.

BraK
12-27-2010, 07:01 AM
Very nice Frement it's a good addition to the tut section.

~BraK

Harry
12-27-2010, 07:30 AM
You should make a default one, so a user can go /stats.php?scriptid=122 - and it will print the stats of that script in a default template.

Thanks for the info.

Frement
12-27-2010, 07:42 AM
You should make a default one, so a user can go /stats.php?scriptid=122 - and it will print the stats of that script in a default template.

Thanks for the info.

The problem with this is, that the script might have different names of variables, and different amount of variables, but that doesn't mean it isn't possible, just would require some work.


Very nice Frement it's a good addition to the tut section.

Thanks :)

Her0n
12-31-2010, 01:08 AM
ha, very cool. I guess I should start using SRL stats ._.;

EvilChicken!
12-31-2010, 02:21 PM
Read my rep+ comment. :)

Frement
01-03-2011, 10:46 AM
Read my rep+ comment. :)

Thats why I added the "Helpful Resources" ;) No need for me to explain the functions again, I could have copied them from the site, but I didn't think there was any need.

The Claw
01-03-2011, 12:09 PM
Can you do it in Python?

i luffs yeww
01-03-2011, 12:13 PM
Can you do it in Python?

Of course. I might post something about it, but I really need to do my homework. Maybe tonight.

Frement
01-03-2011, 12:18 PM
Can you do it in Python?

Well, I know the syntax, but I have never worked with image manipulation in Python, I could give it a try if I find a suitable place for it, I have so much other stuff to do also.

i luffs yeww
01-03-2011, 12:19 PM
I'll work on it after school today or tomorrow, Frement. :) Don't worry about it. Stick to what you've got on your plate for now.

Frement
01-03-2011, 12:24 PM
I'll work on it after school today or tomorrow, Frement. :) Don't worry about it. Stick to what you've got on your plate for now.

Use the GD module ;)

http://newcenturycomputers.net/projects/gdmodule.html

i luffs yeww
01-03-2011, 12:27 PM
Oohh.. :) Thanks, Frement. I'll see what I can do with it.

Probably. :3

Bobzilla69
01-25-2011, 01:24 PM
Thanks for this thread, i finally got it to work for me.

it had been quite a while since i used PHP and once i stared remembering it was easy.

here is what i came up with (its not very "good looking") (if it doesnt show it means there is a error and a refresh should solve it)

http://www.bobzilla69.ratinator.x10.mx/RatinatorScript.php

image should be above (direct link direct link (http://www.bobzilla69.ratinator.x10.mx/RatinatorScript.php))

Edit: Error message when it doesnt show
The image “http://www.bobzilla69.ratinator.x10.mx/RatinatorScript.php” cannot be displayed, because it contains errors.
in error log it has

[25-Jan-2011 08:44:11] PHP Warning: Invalid argument supplied for foreach() in /home/ratinato/public_html/RatinatorScript.php on line 10

Sgt Soul
01-26-2011, 12:36 AM
is there anyway you can have your personal stats for this sig? :)

Frement
01-26-2011, 01:07 AM
is there anyway you can have your personal stats for this sig? :)

If there is a page for that in the api.

Sex
01-26-2011, 10:06 AM
http://stats.villavu.com/api/user/$USERID

Awkwardsaw
07-03-2011, 01:03 AM
Thanks for the tut :)

It works nice when veiwing it in a separate tab, but the forums doesn't recognise the page as an image.



<?php

header("Content-type: image/png");

$stats = file_get_contents("http://stats.villavu.com/api/script/145");
$stats_array = json_decode($stats, true);

function simplify_vars(&$stats_array) {
foreach ($stats_array["vars"] as $array) {
$stats_array[$array[1]] = $array[0];
}
unset($stats_array["vars"]);
}

simplify_vars($stats_array);

//putenv("GDFONTPATH=".realpath("."));

$img = imagecreatefrompng("mta_sig_1.png");



$color["yellow"] = imagecolorallocate( $img, 255, 255, 0 );
$color["white"] = imagecolorallocate( $img, 255, 255, 255 );
$color["green"] = imagecolorallocate( $img, 0, 255, 128 );
$color["orange"] = imagecolorallocate( $img, 254, 179, 0 );

function rs_color($value) {
if ($value < 100000) {
return "yellow";
} elseif ($value > 100000 && $value < 10000000) {
return "white";
} else {
return "green";
}
}

function rs_value($value) {
if ($value < 100000) {
return $value;
} elseif ($value > 100000 && $value < 10000000) {
return substr($value, 0, strlen($value) - 3)."K";
} else {
return substr($value, 0, strlen($value) - 6)."M";
}
}

imageString($img, 10, 130, 15, $stats_array["time"], $color[rs_color($stats_array["Time"])]);
imageString($img, 10, 130, 28, $stats_array["Enchantment Points (Gained)"], $color[rs_color($stats_array["Enchantment Points (Gained)"])]);
imageString($img, 10, 130, 41, $stats_array["Telekinesis Points (Gained)"], $color[rs_color($stats_array["Telekinesis Points (Gained)"])]);
imageString($img, 10, 360, 15, $stats_array["Magic EXP (Gained)"], $color[rs_color($stats_array["Magic EXP (Gained)"])]);
imageString($img, 10, 360, 28, $stats_array["Graveyard Points (Gained)"], $color[rs_color($stats_array["Graveyard Points (Gained)"])]);
imageString($img, 10, 360, 41, $stats_array["Alchemy Points (Gained)"], $color[rs_color($stats_array["Alchemy Points (Gained)"])]);
imageString($img, 10, 130, 53, $stats_array["last_commit_by:"], $color[rs_color(100000)]);
imageString($img, 10, 335, 55, $stats_array["last_commit_on:"], $color[rs_color(100000)]);



imagepng($img);
imagedestroy($img);




?>

http://awkwardscripts.host56.com/MTA/sig.php

http://awkwardscripts.host56.com/MTA/sig.php

MylesMadness
07-03-2011, 01:21 AM
Thanks for the tut :)

It works nice when veiwing it in a separate tab, but the forums doesn't recognise the page as an image.



<?php

header("Content-type: image/png");

$stats = file_get_contents("http://stats.villavu.com/api/script/145");
$stats_array = json_decode($stats, true);

function simplify_vars(&$stats_array) {
foreach ($stats_array["vars"] as $array) {
$stats_array[$array[1]] = $array[0];
}
unset($stats_array["vars"]);
}

simplify_vars($stats_array);

//putenv("GDFONTPATH=".realpath("."));

$img = imagecreatefrompng("mta_sig_1.png");



$color["yellow"] = imagecolorallocate( $img, 255, 255, 0 );
$color["white"] = imagecolorallocate( $img, 255, 255, 255 );
$color["green"] = imagecolorallocate( $img, 0, 255, 128 );
$color["orange"] = imagecolorallocate( $img, 254, 179, 0 );

function rs_color($value) {
if ($value < 100000) {
return "yellow";
} elseif ($value > 100000 && $value < 10000000) {
return "white";
} else {
return "green";
}
}

function rs_value($value) {
if ($value < 100000) {
return $value;
} elseif ($value > 100000 && $value < 10000000) {
return substr($value, 0, strlen($value) - 3)."K";
} else {
return substr($value, 0, strlen($value) - 6)."M";
}
}

imageString($img, 10, 130, 15, $stats_array["time"], $color[rs_color($stats_array["Time"])]);
imageString($img, 10, 130, 28, $stats_array["Enchantment Points (Gained)"], $color[rs_color($stats_array["Enchantment Points (Gained)"])]);
imageString($img, 10, 130, 41, $stats_array["Telekinesis Points (Gained)"], $color[rs_color($stats_array["Telekinesis Points (Gained)"])]);
imageString($img, 10, 360, 15, $stats_array["Magic EXP (Gained)"], $color[rs_color($stats_array["Magic EXP (Gained)"])]);
imageString($img, 10, 360, 28, $stats_array["Graveyard Points (Gained)"], $color[rs_color($stats_array["Graveyard Points (Gained)"])]);
imageString($img, 10, 360, 41, $stats_array["Alchemy Points (Gained)"], $color[rs_color($stats_array["Alchemy Points (Gained)"])]);
imageString($img, 10, 130, 53, $stats_array["last_commit_by:"], $color[rs_color(100000)]);
imageString($img, 10, 335, 55, $stats_array["last_commit_on:"], $color[rs_color(100000)]);



imagepng($img);
imagedestroy($img);




?>

http://awkwardscripts.host56.com/MTA/sig.php

http://awkwardscripts.host56.com/MTA/sig.phpI see the image in your sig and the post just fine?

Awkwardsaw
07-03-2011, 01:23 AM
I see the image in your sig and the post just fine?

:o

it just changed then, it was red x when/ after i posted

Frement
07-03-2011, 01:42 AM
Nice job! :) Glad to see this is of some use.

Zyt3x
07-03-2011, 01:43 AM
:o

it just changed then, it was red x when/ after i postedThat was because 000webhost was checking his account for malicious code

Awkwardsaw
07-03-2011, 01:46 AM
That was because 000webhost was checking his account for malicious code

but why would it work when veiwing it in a separate window?

anyways, atleast i know it works :p

Zyt3x
07-03-2011, 12:49 PM
but why would it work when veiwing it in a separate window?

anyways, atleast i know it works :pMaybe because of your DNS Cache or something? I'm not sure..

Wizzup?
07-03-2011, 12:59 PM
When I get some time I'm sure I can set up a service on villavu that could generate signatures if by that time we still don't have a general service.

Flight
07-03-2011, 01:01 PM
When I get some time I'm sure I can set up a service on villavu that could generate signatures if by that time we still don't have a general service.

:spot:

Smarter Child
07-05-2011, 05:15 AM
When I get some time I'm sure I can set up a service on villavu that could generate signatures if by that time we still don't have a general service.

I would love you for that. :wub::wub::wub::wub::wub::wub:

putonajonny
05-27-2012, 01:27 PM
Thank you very much for this tutorial, I made the image in my sig with it :)

Frement
05-27-2012, 01:30 PM
Thank you very much for this tutorial, I made the image in my sig with it :)

I'm glad it's of some use :)