Header( "Content-type: image/gif");
require( "colors.php");
// 装载GD-Library
// Windows下是这条语句:dl("php3_gd.dll");
// 而 UNIX 就应该是下面这条:
dl( "php3_gd.so");
// 一些初始设定
$Font = 5;
$BGColor = GetColor( "black");
$BGTrans = False;
$FGColor = GetColor( "white");
$FGTrans = False;
$Length = 7;
// 取得输入参数
$query_string = getenv( "QUERY_STRING");
// 分析输入的参数
// 并分闻参数
$env_array = split( "&", $query_string);
// 把参数及其值分开,并把值转成 %XX的形式
while (list($key, $val) = each($env_array))
{
list($name, $wert) = split( "=", $val);
$name = urldecode($name);
$wert = urldecode($wert);
// 写入 $cgivars
$CGIVars[$name] = $wert;
}
// 用输入的参数重新设定初始值
if ($CGIVars[ "BGColor"])
{
if (ereg( "([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars[ "BGColor"], $tmp))
{
$BGColor[ "red"] = $tmp[1];
$BGColor[ "green"] = $tmp[2];
$BGColor[ "blue"] = $tmp[3];
}
else if (eregi( "transparent", $CGIVars[ "BGColor"]))
{
$BGTrans = True;
}
else
{
$BGColor = GetColor($CGIVars[ "BGColor"]);
}
}
if ($CGIVars[ "FGColor"])
{
if (ereg( "([0-9]*) ([0-9]*) ([0-9]*)", $CGIVars[ "FGColor"], $tmp))
{
$FGColor[ "red"] = $tmp[1];
$FGColor[ "green"] = $tmp[2];
$FGColor[ "blue"] = $tmp[3];
}
else if (eregi( "transparent", $CGIVars[ "FGColor"]))
{
$FGTrans = True;
}
else
{
$FGColor = GetColor($CGIVars[ "FGColor"]);
}
}
if ($CGIVars[ "Length"])
{
$Length = $CGIVars[ "Length"];
}
$SizeX = $Length * 13;
$SizeY = 16;
// 读入 counter-文件
if (file_exists( "counter.txt"))
{
$fp = fopen( "counter.txt", "rt");
while ($Line = fgets($fp, 999))
{
if (ereg( "([^ ]*) *([0-9]*)", $Line, $tmp))
{
$CArr[ "$tmp[1]"] = $tmp[2];
}
}
fclose($fp);
// 得到计数器的标志
$Counter = $CArr[$CGIVars[ "Identifier"]];
$Counter += 1;
$CArr[$CGIVars[ "Identifier"]] = $Counter;
}
else
{
// 新的计数器初值为1
$CArr[$CGIVars[ "Identifier"]] = 1;
$Counter = 1;
}
// 写入计数器文件中
$fp = fopen( "counter.txt", "wt");
reset($CArr);
while (list($Key, $Value) = each($CArr))
{
$tmp = sprintf( "%s %dn", $Key, $Value);
fwrite($fp, $tmp);
}
fclose($fp);
$Counter = sprintf( "%0".$Length. "d", $Counter);
// 创建 image
$img = ImageCreate($SizeX + 4, $SizeY + 4);
ImageInterlace($img, 1);
// 透明颜色
$trans = ImageColorAllocate($img, 1, 1, 1);
ImageColorTransparent($img, $trans);
// 填充背景色
if ($BGTrans)
{
ImageFill($img, 1, 1, $trans);
}
else
{
$col = ImageColorAllocate($img, $BGColor[ "red"], $BGColor[ "green"],
$BGColor[ "blue"]);
ImageFill($img, 1, 1, $col);
}
// 输出数字
if ($FGTrans)
{
$col = $trans;
}
else
{
$col = ImageColorAllocate($img, $FGColor[ "red"], $FGColor[ "green"],
$FGColor[ "blue"]);
}
$PosX = 0;
for ($i = 1; $i <= strlen($Counter); $i++)
{
ImageString($img, $Font, $PosX + 3, 2 + $i % 2,
substr($Counter, $i - 1, 1), $col);
if ($i != 1)
{
ImageLine($img, $PosX, 0, $PosX, $SizeY + 4, $trans);
}
$PosX += 13;
}
// 输出图片
ImageGif($img);
ImageDestroy($img);
?>