回目录
计数器程序



<?
/////////////////////////////////////////////////////////////////////////
//
// counter.php - 实现一个很简单的计数器
// 
// 作者: Claus Radloff
// 
// 描述:
// 这个计数器可以变成多重的计数器。因此,每一个 计数器可以自己得到自己
// 的身份证明。
//
// 前面的部分——是背景色以及数字的定义,你可能自行修改。
// 当然,如果背景色是透明的那就更好了。
// 
// 以下是一些在 URL 中的参数:
// 例如:
// Simple Counter:
//   <img src="counter.php?Identifier=Test">
//   
// 红色背景的计数器:
//   <img src="counter.php?Identifier=Test&BGColor=255+0+0">
// 或者是:
//   <img src="counter.php?Identifier=Test&BGColor=red">
//   
// 红色背景,绿色前景,只有4位数的计数器
//   <img src="counter.php?Identifier=Test&BGColor=255+0+0&FGColor=0+255+0&Length=4">
// 或是:
//   <img src="counter.php?Identifier=Test&BGColor=red&FGColor=green&Length=4">
//   
// 透明背景和红色前景的样式:
//   <img src="counter.php?Identifier=Test&BGColor=transparent&FGColor=red">
// 

 4/14/2000    Mouse Chen( litmouse@km169.net   )    Translate it to Chinese


    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 %d\n", $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);
?>


回目录