Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Analog Clock


4 replies to this topic

#1 galexcd

    Trap Grand Marshal Member

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,331 posts
  • Gender:Not Telling
  • myCENT:89.24

Posted 09 January 2008 - 06:46 PM

ok so i was bored and was inspired by the php clock topic to make this dynamic image of an analog clock:
Posted Image



so anyway here's the source code:
<?php
/*
All images and lines of code were written/drawn from scratch by alex...
*/
header("Content-type: image/jpeg");
function newclockhand($time,$color,$length,$thickness){
global $clock;
imagefilledpolygon($clock,array(96,89,99-$thickness+cos(deg2rad($time+90))*2,92+$thickness+sin(deg2rad($time+90))*2,$length*cos(deg2rad($time))+165-$length,$length*sin(deg2rad($time))+165-$length),3,$color);
}
$clock=imagecreatetruecolor(200,200);
$back=imagecreatefromjpeg("back.jpg");
$dot=imagecreatefromjpeg("dot.jpg");
imagecopy($clock,$back,0,0,0,0,200,200);
newclockhand(intval(date("i"))/60*360-90,imagecolorallocate($clock,0,0,0),70,3);
newclockhand(intval(date("g"))/12*360-80+30*intval(date("i"))/60,imagecolorallocate($clock,10,10,10),55,5);
newclockhand(intval(date("s"))/60*360-90,imagecolorallocate($clock,255,0,0),70,1);
imagecopy($clock,$dot,97,91,0,0,6,6);
imagejpeg($clock);
imagedestroy($clock);
?>
and attached are all the images needed.

but if you refresh a bit you can tell there are a few bugs... especially with the thickness of the hands changing. I'm not that great at trig functions, I just know the basic unit circle which I used to make this, but i had to play around with some of the numbers to get it right, but I never got the thickness to work quite right. If any of you math whizzes out there want to improve my code go for it. Also changing the length of the hands affects the time sometimes so make sure you compensate for it by adding degrees within the deg2rad() function.

But other than those 2 the clock works pretty good. Have fun!

Attached Files

  • Attached File  back.jpg   4.01K   32 downloads
  • Attached File  dot.jpg   357bytes   13 downloads


#2 kobra500

    Inane and Inept

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 602 posts
  • Gender:Male
  • Location:UK - Kent
  • myCENT:91.13

Posted 09 January 2008 - 07:19 PM

Good enough... I'm sure they're are better ones but that one works good. Well doen

#3 gogoily

    Member [Level 3]

  • Kontributors
  • PipPipPipPipPipPip
  • 99 posts

Posted 10 January 2008 - 02:02 AM

This code is excellent !!
But I'd like JavaScript clock more, it's real-time and it doesn't use server resource.

#4 galexcd

    Trap Grand Marshal Member

  • [MODERATOR]
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,331 posts
  • Gender:Not Telling
  • myCENT:89.24

Posted 10 January 2008 - 03:40 AM

Yes it isn't very useful but like I said I only did it because I was bored and wanted to do something random in php

#5 Liam_CF

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 403 posts
  • Location:ERROR 404

Posted 10 January 2008 - 05:55 PM

Woah, nice coding. Well done.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users