PHP 制作动态签名图

需要PHP有GD扩展.

代码如下:

<?php  
//Author:BlackGlory
//Date:2011.8.14

// --------------------------------------------------
// 分析返回当前时间
// --------------------------------------------------
function getTime(){  
    return date('Y-m-d H:i:s');
}

// --------------------------------------------------
// 分析返回用户IP地址
// --------------------------------------------------
function getIP()  
{
    if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown"))
        $ip = getenv("HTTP_CLIENT_IP");
    elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown"))
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"),"unknown"))
        $ip = getenv("REMOTE_ADDR");
    elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown"))
        $ip = $_SERVER['REMOTE_ADDR'];
    else
        $ip = "0.0.0.0";
    preg_match('/^d{1,3}.d{1,3}.d{1,3}.d{1,3}/',$ip,$matches);
    return $matches[0];
}

// --------------------------------------------------
// 分析返回用户操作系统名称
// --------------------------------------------------
function getSystem(){  
    $sys=$_SERVER['HTTP_USER_AGENT'];
    if(stripos($sys,"NT 6.1")){
        $os="Windows 7";
    }elseif(stripos($sys,"NT 6.0")){
        $os="Windows Vista";
    }elseif(stripos($sys,"NT 5.1")){
        $os="Windows XP";
    }elseif(stripos($sys,"NT 5.2")){
        $os="Windows Server 2003";
    }elseif(stripos($sys,"NT 5")){
        $os="Windows 2000";
    }elseif(stripos($sys,"NT 4.9")){
        $os="Windows ME";
    }elseif(stripos($sys,"NT 4")){
        $os="Windows NT 4.0";
    }elseif(stripos($sys,"98")){
        $os="Windows 98";
    }elseif(stripos($sys,"95")){
        $os="Windows 95";
    }elseif(stripos($sys,"Mac")){
        $os="Mac";
    }elseif(stripos($sys,"Linux")){
        $os="Linux";
    }elseif(stripos($sys,"Unix")){
        $os="Unix";
    }elseif(stripos($sys,"FreeBSD")){
        $os="FreeBSD";
    }elseif(stripos($sys,"SunOS")){
        $os="SunOS";
    }elseif(stripos($sys,"BeOS")){
        $os="BeOS";
    }elseif(stripos($sys,"OS/2")){
        $os="OS/2";
    }elseif(stripos($sys,"PC")){
        $os="Macintosh";
    }elseif(stripos($sys,"AIX")){
        $os="AIX";
    }else{
        $os="Unknown";
    }

    return $os;
}

// --------------------------------------------------
// 分析返回用户网页浏览器名称
// --------------------------------------------------
function getBrowser(){  
    $sys=$_SERVER['HTTP_USER_AGENT'];
    if(stripos($sys,"NetCaptor") > 0){
        $exp="NetCaptor";
    }elseif(stripos($sys,"Firefox/") > 0){
        preg_match("/Firefox/([^;)]+)+/i",$sys,$b);
        $exp="Mozilla Firefox ".$b[1];
    }elseif(stripos($sys,"MAXTHON") > 0){
        preg_match("/MAXTHONs+([^;)]+)+/i",$sys,$b);
        preg_match("/MSIEs+([^;)]+)+/i",$sys,$ie);
        $exp=$b[0]."IE".$ie[1].")";
    }elseif(stripos($sys,"MSIE") > 0){
        preg_match("/MSIEs+([^;)]+)+/i",$sys,$ie);
        $exp="Internet Explorer ".$ie[1];
    }elseif(stripos($sys,"Netscape") > 0){
        $exp="Netscape";
    }elseif(stripos($sys,"Opera") > 0){
        $exp="Opera";
    }elseif(stripos($sys,"Chrome")){
        $exp="Chrome";
    }else{
        $exp="Unknown";
    }

    return $exp;
}

// --------------------------------------------------
// 绘制图片
// --------------------------------------------------
$image=imagecreate(500,200);
$back_color=imagecolorallocate($image,0,0,0);//黑色
$drawing_color=imagecolorallocate($image,0,100,0);//深绿色
imagerectangle($image,50,40,450,160,$drawing_color);//边框  
imagestring($image,15,70,60,'OS:'.getSystem(),$drawing_color);  
imagestring($image,15,70,80,'Browser:'.getBrowser(),$drawing_color);  
imagestring($image,15,70,100,'IP:'.getIP(),$drawing_color);  
imagestring($image,15,70,120,'Now:'.getTime(),$drawing_color);  
imagestring($image,15,500-strlen('www.blackglory.co.cc')*imagefontwidth(15),200-2*imagefontheight(15),'www.blackglory.co.cc',$drawing_color);  
imagestring($image,15,500-strlen('By BlackGlory')*imagefontwidth(15),200-imagefontheight(15),'By BlackGlory',$drawing_color);

// --------------------------------------------------
// 修改Content-type,作为图片返回
// --------------------------------------------------
Header('Content-type: image/png');  
imagepng($image);  
imagedestroy($image);//销毁

?>