vBulletin Dynamic Signature with PHP and GD
Welcome to our First Tutorial
First we need to make a new PHP Document
<?php
?>
Now we need a variable to tell us what the head will say <yourforum> Statistics
<?php
$forumname = "CodeCall";
?>
now we need to setup the vBulletin Constants using
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
define('THIS_SCRIPT', 'sigTut');
Your script should look like this so far
<?php
$forumname = "CodeCall";
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
define('THIS_SCRIPT', 'sig');
?>
Now to get Forum Statistics we do some SQL Queries like the following
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'];
Now we need a way to detect the users browser. I use a little function I found on PHP.NET
// 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;
}
Lets take a look at the script so far =]
<?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 we create a new PNG image. I prefer to use a function like this
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;
}
Now all thats left to do is load the image and add the data like so
// 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);
Congrats you have a working dynamic sig. Here is the final 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);
?>
Category : PHP Tutorials | 2009-11-15 00:04 | Comments (1) |




