Jump to content
xisto Community
Sign in to follow this  
galexcd

Analog Clock

Recommended Posts

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!

post-12251-1199904162_thumb.jpg

post-12251-1199904176_thumb.jpg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.