Frement
2010-12-26 23:48:05
So, the new SRL Stats system is out, and there is a nice API, why not make stats signatures?
Things you need:
Lets start by having a look at the API at http://stats.villavu.com/api/script/2.
The syntax is JSON, and PHP has built-in parser for it.
Parse & Clean
Parsing the data from stats API into array:
[PHP]<?php
$stats = file_get_contents("http://stats.villavu.com/api/script/2");
$stats_array = json_decode($stats, true);
?>[/PHP]
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:
Now we might want to put "vars" array into more easily accessible form. I made a simplify_vars() function for it.
[PHP]<?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);
?>[/PHP]
Output:
Now that we have our array cleaned up, we can move to the fun part.
Display Stats
Setting font path:
[PHP]putenv("GDFONTPATH=".realpath("."));[/PHP]
Creating image:
[PHP]$img = imagecreatefrompng("sig_base_example.png");[/PHP]
I already have some colors ready that are used in RS, but you can allocate your own colors easily with:
[PHP]imagecolorallocate($img, Red, Green, Blue);[/PHP]
Now that you have allocated your colors, you should have something like this:
[PHP]<?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 );
?>[/PHP]
You might want to color your values differently, like RS does for item quantity, so here is two functions that you can use:
[PHP]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";
}
}[/PHP]
Drawing text on your image is easy, one function does it all:
[PHP]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"]));[/PHP]
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():
[PHP]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);
}[/PHP]
When you have drawn all your texts, you are ready to show the image.
[PHP]imagepng($img);
imagedestroy($img);[/PHP]
Now some final things, and you are ready. You need to specify headers for your content, which is image/png, so:
[PHP]header("Content-type: image/png");[/PHP]
Now your script should look like this:
[PHP]<?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);
?>[/PHP]
And it should output this:

Helpful Resources
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!
- 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]<?php
$stats = file_get_contents("http://stats.villavu.com/api/script/2");
$stats_array = json_decode($stats, true);
?>[/PHP]
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]<?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);
?>[/PHP]
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:
[PHP]putenv("GDFONTPATH=".realpath("."));[/PHP]
Creating image:
[PHP]$img = imagecreatefrompng("sig_base_example.png");[/PHP]
I already have some colors ready that are used in RS, but you can allocate your own colors easily with:
[PHP]imagecolorallocate($img, Red, Green, Blue);[/PHP]
Now that you have allocated your colors, you should have something like this:
[PHP]<?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 );
?>[/PHP]
You might want to color your values differently, like RS does for item quantity, so here is two functions that you can use:
[PHP]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";
}
}[/PHP]
Drawing text on your image is easy, one function does it all:
[PHP]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"]));[/PHP]
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():
[PHP]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);
}[/PHP]
When you have drawn all your texts, you are ready to show the image.
[PHP]imagepng($img);
imagedestroy($img);[/PHP]
Now some final things, and you are ready. You need to specify headers for your content, which is image/png, so:
[PHP]header("Content-type: image/png");[/PHP]
Now your script should look like this:
[PHP]<?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);
?>[/PHP]
And it should output this:
Helpful Resources