用php绘画时钟

<?php
header("Content-type: image/png");
$hour = date('h');
//获得小时
$min = date('i');
//获得分钟
$p = 3.14159;
//定义PI值
$th = ((30*$hour) $min/2)*((2*$p)/360);
$tm = ($min*6)*((2*$p)/360);
//换算图形化时钟上时针和分针所在的位置
$image = imagecreate(200,200);
//创建图像
$colorbk = imagecolorallocate($image,200,200,200);
//设置背景色
$color = imagecolorallocate($image,225,0,0);
//设置前景色
$colorY = imagecolorallocate($image,225,0,255);
//设置字体颜色
$colorGreen = imagecolorallocate($image,0,225,0);
//设置表盘边框颜色
$colorBlue = imagecolorallocate($image,0,0,225);
//设置表盘颜色
imagefill($image,0,0,$colorbk);
//用背景色填充图像
for($i=0;$i<12;$i  ){
//绘制表盘
$a = $i*$p/6; // ( 说是这行错误 )
$x =100   sin($a)*100;
$y =100 - cos($a)*100;
//画圆弧
imagearc ($image, $x, $y, 5, 5, 0, 360, $color);
//在图上书写字符串
imagestring($image, 4, $x, $y, $i,$color);
}
imagearc($image,100,100,200,200,0,360,$colorGreen);
//填充
//imagefill($image,100,100,$colorBlue);
imagearc($image,100,100,10,10,0,360,$color);
imagefill($image,100,100,$color);
//画线
$x_h=100 sin($th)*60;
$y_h=100-cos($th)*60;
imageline($image,100,100,$x_h,$y_h,$color);
$x_m =100 sin($tm)*80;
$y_m =100-cos($tm)*80;
imageline($image,100,100,$x_m,$y_m,$color);
//显示图像
imagepng($image);
//释放图像占用的资源
imagedestroy($image);
?>

演示
sj.php
回复列表(12|隐藏机器人聊天)
  • @Ta / 2012-11-28 / /
    附件没弄…
  • @Ta / 2012-11-28 / /
    顶,收藏了
  • @Ta / 2012-11-28 / /
    收藏了,多谢
  • @Ta / 2012-11-28 / /
    可以封装
  • @Ta / 2012-11-28 / /
    时间正确吗???
  • @Ta / 2012-11-28 / /
    顶了。
  • @Ta / 2012-11-28 / /
    我觉得不需要封装,没必要,因为你没什么参数可传的,再说了封装了怎么用?这种图片形式的php文件很特别,一般调用都是用<img  src='xx.php'>标签调用的~
  • @Ta / 2012-11-28 / /
    有点不会看时间了…长的是小时?
    反正时间肯定不准
  • @Ta / 2012-11-28 / /
    不错
  • @Ta / 2012-11-28 / /
    代码中少了许多 号,应该是UC的问题,正确代码
    <?php
    header("Content-type: image/png");
    $hour = date('h');
    //获得小时
    $min = date('i');
    //获得分钟
    $p = 3.14159;
    //定义PI值
    $th = ((30*$hour) $min/2)*((2*$p)/360);
    $tm = ($min*6)*((2*$p)/360);
    //换算图形化时钟上时针和分针所在的位置
    $image = imagecreate(200,200);
    //创建图像
    $colorbk = imagecolorallocate($image,200,200,200);
    //设置背景色
    $color = imagecolorallocate($image,225,0,0);
    //设置前景色
    $colorY = imagecolorallocate($image,225,0,255);
    //设置字体颜色
    $colorGreen = imagecolorallocate($image,0,225,0);
    //设置表盘边框颜色
    $colorBlue = imagecolorallocate($image,0,0,225);
    //设置表盘颜色
    imagefill($image,0,0,$colorbk);
    //用背景色填充图像
    for($i=0;$i<12;$i  ){
    //绘制表盘
    $a = $i*$p/6; // ( 说是这行错误 )
    $x =100  sin($a)*100;
    $y =100 - cos($a)*100;
    //画圆弧
    imagearc ($image, $x, $y, 5, 5, 0, 360, $color);
    //在图上书写字符串
    imagestring($image, 4, $x, $y,$i,$color);
    }
    imagearc($image,100,100,200,200,0,360,$colorGreen);
    //填充
    //imagefill($image,100,100,$colorBlue);
    imagearc($image,100,100,10,10,0,360,$color);
    imagefill($image,100,100,$color);
    //画线
    $x_h=100 sin($th)*60;
    $y_h=100-cos($th)*60;
    imageline($image,100,100,$x_h,$y_h,$color);
    $x_m =100 sin($tm)*80;
    $y_m =100-cos($tm)*80;
    imageline($image,100,100,$x_m,$y_m,$color);
    //显示图像
    imagepng($image);
    //释放图像占用的资源
    imagedestroy($image);
    ?>
  • @Ta / 2012-11-28 / /
    代码中少了许多+号,应该是UC的问题,正确代码
    <?php
    header("Content-type: image/png");
    $hour = date('h');
    //获得小时
    $min = date('i');
    //获得分钟
    $p = 3.14159;
    //定义PI值
    $th = ((30*$hour)+$min/2)*((2*$p)/360);
    $tm = ($min*6)*((2*$p)/360);
    //换算图形化时钟上时针和分针所在的位置
    $image = imagecreate(200,200);
    //创建图像
    $colorbk = imagecolorallocate($image,200,200,200);
    //设置背景色
    $color = imagecolorallocate($image,225,0,0);
    //设置前景色
    $colorY = imagecolorallocate($image,225,0,255);
    //设置字体颜色
    $colorGreen = imagecolorallocate($image,0,225,0);
    //设置表盘边框颜色
    $colorBlue = imagecolorallocate($image,0,0,225);
    //设置表盘颜色
    imagefill($image,0,0,$colorbk);
    //用背景色填充图像
    for($i=0;$i<12;$i++){
    //绘制表盘
    $a = $i*$p/6; // ( 说是这行错误 )
    $x =100 +sin($a)*100;
    $y =100 - cos($a)*100;
    //画圆弧
    imagearc ($image, $x, $y, 5, 5, 0, 360, $color);
    //在图上书写字符串
    imagestring($image, 4, $x, $y,$i,$color);
    }
    imagearc($image,100,100,200,200,0,360,$colorGreen);
    //填充
    //imagefill($image,100,100,$colorBlue);
    imagearc($image,100,100,10,10,0,360,$color);
    imagefill($image,100,100,$color);
    //画线
    $x_h=100+sin($th)*60;
    $y_h=100-cos($th)*60;
    imageline($image,100,100,$x_h,$y_h,$color);
    $x_m =100+sin($tm)*80;
    $y_m =100-cos($tm)*80;
    imageline($image,100,100,$x_m,$y_m,$color);
    //显示图像
    imagepng($image);
    //释放图像占用的资源
    imagedestroy($image);
    ?>
  • @Ta / 2012-12-01 / /
    @夏夜-低调,M_PI常量低调地路过
添加新回复
回复需要登录