Skip to content

Instantly share code, notes, and snippets.

@Bluscream
Last active October 20, 2022 10:51
Show Gist options
  • Save Bluscream/d103f9791e5300f22976681832a77ac5 to your computer and use it in GitHub Desktop.
Save Bluscream/d103f9791e5300f22976681832a77ac5 to your computer and use it in GitHub Desktop.
php jpgraph steamcharts image generator
# AddHandler application/x-httpd-php .jpg
RewriteEngine on
# RewriteCond %{QUERY_STRING} ^$
RewriteRule ^bs\..*$ steam.php?max=49&height=375&width=600&bg&%{QUERY_STRING} [L]
RewriteRule ^csgo\..*$ steam.php?max=49&height=375&width=600&bg&appid=730&title=CS:GO&%{QUERY_STRING} [L]
RewriteRule ^steam\..*$ steam.php?%{QUERY_STRING} [L]
<?php // content="text/plain; charset=utf-8"
// # vim: set syntax=php:
// https://minopia.de/chart/steam.jpg?max=100&appid=550650&title=Black%20Squad&height=200&width=600&legend&bg&color=darkgray
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
header("Refresh: 60;");
// Requires: https://jpgraph.net/download/
require_once('jpgraph/jpgraph.php');
require_once('jpgraph/jpgraph_line.php');
require_once('jpgraph/jpgraph_bar.php');
require_once('jpgraph/jpgraph_date.php');
function get($key, $default=NULL) {
return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}
try {
$max = intval(get("max", 15));
$appid = get("appid", "550650");
$data = json_decode(file_get_contents("https://steamcharts.com/app/$appid/chart-data.json"));
$data = array_slice($data, -$max);
$datax = array();
$ydata = array();
$highest = 0;
foreach ($data as $key => $value) {
$players = intval($value[1]);
array_push($datax, intval($value[0]) / 1000);
array_push($ydata, $players);
if ($players > $highest) {
$highest = $players;
}
}
// if ($max > 15) {
// foreach (range(1, count($datax), 2) as $key) {
// $datax[$key] = 0;
// }
// }
} catch (Exception $e) {
throw new JpGraphException('Can not get Chart data');
}
// Create a graph instance
$graph = new Graph(get("width",600),get("height",250));
$graph->clearTheme();
//$graph->SetShadow();
// $graph->SetBox(true,'green',2);
$color = get("color", false);
if ($color) {
$graph->SetMarginColor($color);
$graph->SetColor($color);
}
// Specify what scale we want to use,
// int = integer scale for the X-axis
// int = integer scale for the Y-axis
$graph->SetScale('datlin', 0, $highest + 100);
// $graph->yaxis->scale->ticks->Set(50000);
$graph->xaxis->SetLabelAngle(45);
if ($max > 100) {
$graph->xaxis->scale->SetDateFormat( 'd.m.y' );
$graph->SetMargin(50,10,0,50);
} else if ($max > 15) {
$graph->xaxis->scale->SetDateFormat( 'd.m. - H:i' );
$graph->SetMargin(50,10,40,70);
}
// Setup a title for the graph
$title = get("title", "Black Squad ({appid}) - {date} - {players} players");
$title = str_replace("{date}", date("d.m.Y H:i:s"), $title);
$title = str_replace("{players}", end($ydata), $title);
$title = str_replace("{appid}", $appid, $title);
$graph->title->Set($title);
if ($appid === "550650") {
$graph->subtitle->Set(substr(end(json_decode(file_get_contents("/var/www/blacksquad/news.json"))), 0, 75));
}
if (isset($_GET['legend'])) {
// Setup titles and X-axis labels
$graph->xaxis->title->Set('Time');
// Setup Y-axis title
$graph->yaxis->title->Set('Players');
}
if (isset($_GET['bg'])) {
$graph->SetBackgroundImage("https://cdn.akamai.steamstatic.com/steam/apps/$appid/header.jpg", BGIMG_FILLFRAME);
$graph->title->SetColor('white');
$graph->subtitle->SetColor('white');
// $graph->title->SetShadow('gray',0);
// $graph->subtitle->SetShadow('gray',0);
$graph->xaxis->SetColor('white');
$graph->yaxis->SetColor('white');
$graph->xaxis->title->SetColor('white');
$graph->yaxis->title->SetColor('white');
}
if (isset($_GET['t'])) {
$graph->SetMarginColor('white:0');
$graph->SetFrame(true,'white:0', 1); //but this make labels/letters weird !!!
$graph->img->SetTransparent('white');
}
// Create the linear plot
$lineplot=new LinePlot($ydata, $datax);
$lineplot->SetFillColor('orange@0.5');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
header("Refresh: 60;");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment