+ Reply to Thread
Results 1 to 2 of 2

Thread: vBulletin GD Stat Sig

  1. #1
    Learning Programmer Affix has a spectacular aura about Affix has a spectacular aura about
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    47
    Blog Entries
    2

    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

    Code:
    <?php

    ?>
    Now we need a variable to tell us what the head will say <yourforum> Statistics

    Code:
    <?php

    $forumname 
    "CodeCall";

    ?>
    now we need to setup the vBulletin Constants using

    Code:
    define('CWD', (($getcwd getcwd()) ? $getcwd '.'));
    define('THIS_SCRIPT''sigTut'); 
    Your script should look like this so far

    Code:
    <?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

    Code:
    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

    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;
        } 
    Lets take a look at the script so far =]

    Code:
    <?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

    Code:
    function LoadPNG($imgname)
    {
       
    //$im = @imagecreatefrompng($imgname); /* Attempt to open */
       
    $im = @imagecreate(25080);
        
    $background_color imagecolorallocate($im000);
       if (!
    $im) { /* See if it failed */
           
    $im  imagecreatetruecolor(15030); /* Create a blank image */
           
    $bgc imagecolorallocate($im255255255);
           
    $tc  imagecolorallocate($im000);
           
    imagefilledrectangle($im0015030$bgc);
           
    /* Output an errmsg */
           
    imagestring($im155"Error loading $imgname"$tc);
       }
       return 
    $im;

    Now all thats left to do is load the image and add the data like so

    Code:
    // Actually Loads the image
    $im = @LoadPNG('base.png');


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

    //Defines Colors
    $text_color imagecolorallocate($im02550);

    // Adds Text
    imagestring($im1705,  $forumname " Statistics"$text_color);
    imagestring($im1515,  "Threads: " $totalthreads$text_color);
    imagestring($im1525,  "Posts: " $totalposts$text_color);
    imagestring($im1535,  "Users: " $totalusers$text_color);
    imagestring($im1545,  "You Are : "$text_color);
    imagestring($im1555,  $ip$text_color);
    imagestring($im1565,  get_user_browser(), $text_color);



    //Outputs the image
    imagepng($im);
    imagedestroy($im); 
    Congrats you have a working dynamic sig. Here is the final code

    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 ]

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: vBulletin GD Stat Sig

    Now that is a cool tutorial. +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. vBulletin Statistics for Joomla!
    By Jordan in forum Community Projects
    Replies: 16
    Last Post: 05-29-2009, 08:14 PM
  2. Logging into vBulletin using cURL
    By Affix in forum PHP Tutorials
    Replies: 21
    Last Post: 04-21-2009, 12:17 AM
  3. vBulletin Templates?
    By phpforfun in forum The Lounge
    Replies: 0
    Last Post: 02-04-2009, 05:23 PM
  4. vBulletin No Replies for Joomla!
    By Jordan in forum Community Projects
    Replies: 10
    Last Post: 01-01-2009, 02:49 PM
  5. Lot's of errors
    By Jaan in forum PHP Forum
    Replies: 11
    Last Post: 03-08-2008, 10:46 AM