[gd library] Bezoekers teller, grafisch
dit script is Object georiƫnteerd geprogrammeerd. Maakt gebruik van de php image functies en je database. /* CREATE TABLE `count` ( `id` int(11) NOT NULL auto_increment, `time` int(11) NOT NULL default '0', `ip` mediumtext NOT NULL, UNIQUE KEY `id` (`id`) ) */ Aanmaak. relatief simpel: /* $Teller = new QCounter() $Teller->__construct( string Host, string Db_user, string Db_password, string Db_database, HtmlColor achtergrond, HtmlColor tekstkleur, HtmlColor grafiek, HtmlColor indexering-text, HtmlColor indexering-streep, int x, int y ); */ type string = "hoiblaatblub"; die je wel kent. met HtmlColor bedoel ik "#EFEFEF" of iets dergelijks. dus een voorbeeld: /* $count = new QCounter(); $count->__construct("localhost", "fg", "fg", "Josko", "#E6E6E6", "#000000", "#FF0000", "#000000", "#333333", 400, 500); */ preview is voorlopig te zien op: http://www.zchosting.nl/Storage/GraphicCount.php?tryout=true deze test zit ook in het script zelf.
<?php
################################################################################
# * Name: Josko de Boer #
# * E-mail: [email protected] #
# * country: The Netherlands #
# * company: ZcHosting #
# * language: Php + Mysql #
# * using gd libaries #
################################################################################
function QArrayRGB($Col)
{
$result = array();
$result[] = hexdec($Col{1}.$Col{2});
$result[] = hexdec($Col{3}.$Col{4});
$result[] = hexdec($Col{5}.$Col{6});
return $result;
}
//---- function for getting heights ---//
function Height($number, $max, $xCall)
{
//-- count visits ---//
$x = $number;
//-- people each px -- //
$Px = $max / $xCall;
//--- height ---//
$H = $x / $Px;
return round($H, 0);
}
//---- the class for the counter ---------//
class QCounter
{
//---- database vars ---------//
var $host;
var $user;
var $password;
var $db;
//---- current date, timestamp on 12:00:00 ---------//
var $date;
//---- sizes ---------//
var $x;
var $y;
//---- Colors ---------//
var $bgcolor;
var $fontcolor;
var $linecolor;
var $commentcolor;
var $indexcolor;
//---- constructor ---------//
function __construct()
{
if(func_num_args() == 0) exit("You <b> must</b> give a load of vars for this.<br \>\n You still are to do that.");
//---- autoloading some static vars ---------//
$this->host = func_get_arg(0);
$this->user = func_get_arg(1);
$this->password = func_get_arg(2);
$this->db = func_get_arg(3);
$this->date = mktime(
0,
0,
0,
date("m"),
date("d"),
date("y")
);
$this->bgcolor = func_get_arg(4);
$this->fontcolor = func_get_arg(5);
$this->linecolor = func_get_arg(6);
$this->commentcolor = func_get_arg(7);
$this->indexcolor = func_get_arg(8);
$this->bgcolor = QArrayRGB( $this->bgcolor);
$this->fontcolor = QArrayRGB( $this->fontcolor);
$this->linecolor = QArrayRGB( $this->linecolor);
$this->commentcolor = QArrayRGB( $this->commentcolor);
$this->indexcolor = QArrayRGB( $this->indexcolor);
$this->x = func_get_arg(9);
$this->y = func_get_arg(10);
//---- calling functions ----------//
$this->__connect();
$Verification = $this->__logger();
if( $Verification)
{
$this->__image();
}
}
//---- connection ---------//
function __connect()
{
mysql_connect($this->host, $this->user, $this->password) or die(mysql_error());
mysql_select_db($this->db) or die(mysql_error());
}
//---- add record ---------//
function __logger()
{
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "SELECT * FROM count WHERE time='".$this->date."' AND ip='".$ip."'";
$query = mysql_query($sql) or die(mysql_error());
$result = mysql_num_rows($query);
if($result == 0)
{
$sql = "INSERT INTO count (time, ip) VALUES ('".$this->date."', '".$ip."')";
mysql_query($sql) or die(mysql_error());
return false;
}
else
{
return true;
}
}
//----- This is the main function, giving the image ---- //
function __image()
{
$ImagePng = imagecreate($this->x, $this->y);
$error = false;
//--- allocating colors -----//
$this->bgcolor = imagecolorallocate(
$ImagePng,
$this->bgcolor[0],
$this->bgcolor[1],
$this->bgcolor[2]
);
$this->fontcolor = imagecolorallocate(
$ImagePng,
$this->fontcolor[0],
$this->fontcolor[1],
$this->fontcolor[2]
);
$this->linecolor = imagecolorallocate(
$ImagePng,
$this->linecolor[0],
$this->linecolor[1],
$this->linecolor[2]
);
$this->commentcolor = imagecolorallocate(
$ImagePng,
$this->commentcolor[0],
$this->commentcolor[1],
$this->commentcolor[2]
);
$this->indexcolor = imagecolorallocate(
$ImagePng,
$this->indexcolor[0],
$this->indexcolor[1],
$this->indexcolor[2]
);
if($_GET['tryout'] == "true")
{
$max = rand( 100, 10000);
$logs = array(
rand(10, $max),
rand(10, $max),
rand(10, $max),
rand(10, $max),
rand(10, $max),
rand(10, $max),
rand(10, $max)
);
}
else
{
$logs = $this->__load();
}
$xCall = ($this->x-50) / count($logs);
$yCall = ($this->y-50);
$PeopleStripe = max($logs) / 20;
$yStripe = $yCall / 20;
$xStripe = 5;
for($a = 0; $a <= 20; $a++)
{
$cury = $a * $yStripe + 45;
imageline($ImagePng, 5, $cury, 5+$xStripe, $cury, $this->indexcolor);
$number = (20*$PeopleStripe)-($a*$PeopleStripe);
imagestring($ImagePng, 100, 15, $cury-5, round($number, 0), $this->commentcolor);
}
imageline($ImagePng, 50, 0, 50, $this->y, $this->indexcolor);
$iterance = 0;
foreach($logs AS $visit)
{
$iterance++;
$xCur = 55 + ( $iterance - 1) * $xCall;
//--- drawing line ---//
$yCur = $this->y - Height($visit, max($logs), $yCall);
$yNext = $this->y - Height($logs[$iterance], max($logs), $yCall);
$xNext = $xCur + $xCall;
if($iterance != 7)
imageline($ImagePng, $xCur, $yCur, $xNext, $yNext, $this->linecolor);
//----- text -----/
imagestring($ImagePng, 100, $xCur, 15, $visit, $this->fontcolor);
imagestring($ImagePng, 100, $xCur, 35, $iterance, $this->fontcolor);
}
header("Content-type: image/png");
imagepng($ImagePng);
imagedestroy($ImagePng);
}
function __load()
{
$array = array();
for($a = 0; $a < 7; $a++)
{
$ddate = $this->date;
$ddate -= $a * 60 * 60 * 24;
$query = mysql_query("SELECT * FROM count WHERE time='".$ddate."'") or die(mysql_error());
$array[] = mysql_num_rows($query);
}
return $array;
}
}
?>
Reacties
0