Sian 发表于 2016-1-6 10:06:06

PHP中使用PD库实现动态时针


主体程序:index.php
<?php
        // 设置系统时区
        date_default_timezone_set("PRC");
        $h = date("H");
        $i = date("i");
        $s = date("s");
        // 标识当前资源为图片让浏览器直接解析
        header("Content-type: image/png");
        // 创建一个图层200x200
        $image = imagecreatetruecolor(250, 250);
        // 创建两个颜色变量
        $red = imagecolorallocate($image, 0xFF, 0, 0);
        $blue = imagecolorallocate($image, 0, 0, 0xFF);
        $pink = imagecolorallocate($image, 0xFF, 0, 0xFF);
        $white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
        $blank = imagecolorallocate($image, 0, 0, 0);
        // 背景
        imagefill($image, 0, 0, $white);
        // 边框
        imagerectangle($image, 0, 0, 249, 249, $blank);
        // 时间文字
        imagestring($image, 4, 10, 10, date("Y-m-d H:i:s"), $blank);
        // 表盘
        imageellipse($image, 125, 130, 190, 190, $blue);
        // 表中心
        imageellipse($image, 125, 130, 4, 4, $blue);
        // 表盘文字
        imagestring($image, 3, 118, 40, "12", $blue);
        imagestring($image, 3, 210, 125, "3", $blue);
        imagestring($image, 3, 125, 205, "6", $blue);
        imagestring($image, 3, 35, 123, "9", $blue);

        // 以秒针数来计算指针顶端的坐标位置
        $sx = sin($s*6*pi()/180)*80 + 125;
        $sy = 130 - cos($s*6*pi()/180)*80;
        // 以表盘圆心为起点画到顶端的线条
        imageline($image, 125, 130, $sx, $sy, $red);
        // 分钟
        $ix = sin($i*6*pi()/180)*60 + 125;
        $iy = 130 - cos($i*6*pi()/180)*60;
        imageline($image, 125, 130, $ix, $iy, $blank);
        // 时针
        $h = $h + $i/60;
        $hx = sin($h*30*pi()/180)*40 + 125;
        $hy = 130 - cos($h*30*pi()/180)*40;
        imageline($image, 125, 130, $hx, $hy, $blank);
       
       
        // 当前页面输出图像
        imagegif($image);
        imagedestroy($image);
?>
index.html<img id="time" src="index.php" />

<!--每一秒针调用一次图片-->
<script language="JavaScript">
setInterval(function(){
        document.getElementById("time").src="index.php?"+Math.random();
},1000);
</script>
页: [1]
查看完整版本: PHP中使用PD库实现动态时针