Thread: vBulletin GD Stat Sig |
Waddup,
Affix is back with another PHP Tutorial. This time it shows you how to display vBulletin stats using the GD Image libs for PHP.
First we need to make a new PHP Document
Now we need a variable to tell us what the head will say <yourforum> StatisticsCode:<?php
?>
now we need to setup the vBulletin Constants usingCode:<?php
$forumname = "CodeCall";
?>
Your script should look like this so farCode:define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
define('THIS_SCRIPT', 'sigTut');
Now to get Forum Statistics we do some SQL Queries like the followingCode:<?php
$forumname = "CodeCall";
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
define('THIS_SCRIPT', 'sig');
?>
Now we need a way to detect the users browser. I use a little function I found on PHP.NETCode:require_once(CWD . '/includes/init.php');
$totalthreads = $db->query_first("SELECT COUNT(threadid) FROM " . TABLE_PREFIX . "thread");
$totalthreads = implode(",",$totalthreads);
$totalposts = $db->query_first("SELECT COUNT(postid) FROM " . TABLE_PREFIX . "post");
$totalposts = implode(",",$totalposts);
$totalusers = $db->query_first("SELECT COUNT(userid) FROM " . TABLE_PREFIX . "user");
$totalusers = implode(",",$totalusers);
//Not SQL I know but Meh :)
$ip = $_SERVER['REMOTE_ADDR'];
Lets take a look at the script so far =]Code:// Get Users Browser
function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = 'Unknown';
if(preg_match('/MSIE/i',$u_agent))
{
$ub = "Internet Explorer";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$ub = "Firefox";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$ub = "Safari";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$ub = "Chrome";
}
elseif(preg_match('/Flock/i',$u_agent))
{
$ub = "Flock";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$ub = "Opera";
}
return $ub;
}
Now we create a new PNG image. I prefer to use a function like thisCode:<?php
$forumname = "CodeCall";
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
define('THIS_SCRIPT', 'sig');
require_once(CWD . '/includes/init.php');
$totalthreads = $db->query_first("SELECT COUNT(threadid) FROM " . TABLE_PREFIX . "thread");
$totalthreads = implode(",",$totalthreads);
$totalposts = $db->query_first("SELECT COUNT(postid) FROM " . TABLE_PREFIX . "post");
$totalposts = implode(",",$totalposts);
$totalusers = $db->query_first("SELECT COUNT(userid) FROM " . TABLE_PREFIX . "user");
$totalusers = implode(",",$totalusers);
$username = $_GET['username'];
$ip = $_SERVER['REMOTE_ADDR'];
// Get Users Browser
function get_user_browser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = 'Unknown';
if(preg_match('/MSIE/i',$u_agent))
{
$ub = "Internet Explorer";
}
elseif(preg_match('/Firefox/i',$u_agent))
{
$ub = "Firefox";
}
elseif(preg_match('/Safari/i',$u_agent))
{
$ub = "Safari";
}
elseif(preg_match('/Chrome/i',$u_agent))
{
$ub = "Chrome";
}
elseif(preg_match('/Flock/i',$u_agent))
{
$ub = "Flock";
}
elseif(preg_match('/Opera/i',$u_agent))
{
$ub = "Opera";
}
return $ub;
}
Now all thats left to do is load the image and add the data like soCode:function LoadPNG($imgname)
{
//$im = @imagecreatefrompng($imgname); /* Attempt to open */
$im = @imagecreate(250, 80);
$background_color = imagecolorallocate($im, 0, 0, 0);
if (!$im) { /* See if it failed */
$im = imagecreatetruecolor(150, 30); /* Create a blank image */
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/* Output an errmsg */
imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
}
return $im;
}
Congrats you have a working dynamic sig. Here is the final codeCode:// Actually Loads the image
$im = @LoadPNG('base.png');
//Adds the Header
header("Content-type: image/png");
//Defines Colors
$text_color = imagecolorallocate($im, 0, 255, 0);
// Adds Text
imagestring($im, 1, 70, 5, $forumname . " Statistics", $text_color);
imagestring($im, 1, 5, 15, "Threads: " . $totalthreads, $text_color);
imagestring($im, 1, 5, 25, "Posts: " . $totalposts, $text_color);
imagestring($im, 1, 5, 35, "Users: " . $totalusers, $text_color);
imagestring($im, 1, 5, 45, "You Are : ", $text_color);
imagestring($im, 1, 5, 55, $ip, $text_color);
imagestring($im, 1, 5, 65, get_user_browser(), $text_color);
//Outputs the image
imagepng($im);
imagedestroy($im);
Code:<?php // Affix's Dynamic Sig Tut // Http://root-the.net // http://codecall.net $forumname= "Root-The.NET"; define('CWD', (($getcwd = getcwd()) ? $getcwd : '.')); define('THIS_SCRIPT', 'sig'); require_once(CWD . '/includes/init.php'); $totalthreads = $db->query_first("SELECT COUNT(threadid) FROM " . TABLE_PREFIX . "thread"); $totalthreads = implode(",",$totalthreads); $totalposts = $db->query_first("SELECT COUNT(postid) FROM " . TABLE_PREFIX . "post"); $totalposts = implode(",",$totalposts); $totalusers = $db->query_first("SELECT COUNT(userid) FROM " . TABLE_PREFIX . "user"); $totalusers = implode(",",$totalusers); $username = $_GET['username']; $ip = $_SERVER['REMOTE_ADDR']; // Get Users Browser function get_user_browser() { $u_agent = $_SERVER['HTTP_USER_AGENT']; $ub = 'Unknown'; if(preg_match('/MSIE/i',$u_agent)) { $ub = "Internet Explorer"; } elseif(preg_match('/Firefox/i',$u_agent)) { $ub = "Firefox"; } elseif(preg_match('/Safari/i',$u_agent)) { $ub = "Safari"; } elseif(preg_match('/Chrome/i',$u_agent)) { $ub = "Chrome"; } elseif(preg_match('/Flock/i',$u_agent)) { $ub = "Flock"; } elseif(preg_match('/Opera/i',$u_agent)) { $ub = "Opera"; } return $ub; } // Loads a PNG image function LoadPNG($imgname) { //$im = @imagecreatefrompng($imgname); /* Attempt to open */ $im = @imagecreate(250, 80); $background_color = imagecolorallocate($im, 0, 0, 0); if (!$im) { /* See if it failed */ $im = imagecreatetruecolor(150, 30); /* Create a blank image */ $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */ imagestring($im, 1, 5, 5, "Error loading $imgname", $tc); } return $im; } // Actually Loads the image $im = @LoadPNG('base.png'); //Adds the Header header("Content-type: image/png"); //Defines Colors $text_color = imagecolorallocate($im, 0, 255, 0); // Adds Text imagestring($im, 1, 70, 5, $forumname . " Statistics", $text_color); imagestring($im, 1, 5, 15, "Threads: " . $totalthreads, $text_color); imagestring($im, 1, 5, 25, "Posts: " . $totalposts, $text_color); imagestring($im, 1, 5, 35, "Users: " . $totalusers, $text_color); imagestring($im, 1, 200, 35, $username, $text_color); imagestring($im, 1, 5, 45, "You Are : ", $text_color); imagestring($im, 1, 5, 55, $ip, $text_color); imagestring($im, 1, 5, 65, get_user_browser(), $text_color); //Outputs the image imagepng($im); imagedestroy($im); ?>
Working Example
Play around with it a little and perfect it
Last edited by Affix; 09-08-2009 at 09:03 AM.
[ PHP Guru | C# Coder | Graphics God | Fedora Developer ]
Now that is a cool tutorial. +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)