igogiko

PHP Analog clock

Mar 27th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. $height = 450;
  3. $width = 450;
  4. $img = imagecreatetruecolor($width, $height);
  5.  
  6. $black = imagecolorallocate($img, 0, 0, 0);
  7. $white = imagecolorallocate($img, 255, 255, 255);
  8. $red   = imagecolorallocate($img, 255, 0, 0);
  9. $gray  = imagecolorallocate($img, 105, 105, 105);
  10.  
  11. imagefill($img, 0, 0, $black);
  12.  
  13. $dx = $width / 2; // Zeropoint x
  14. $dy = $height / 2; // Zeropoint y
  15. $hr = 100; // Hour radius
  16. $mr = 120; // Minute radius
  17.  
  18. $hour = date("G", time() + 3600);
  19. $min = intval(date("i", time() + 3600));
  20.  
  21. $hdeg = 90 - 0.5 * ($hour * 60 + $min); // Hour degrees
  22. $mdeg = 90 - 6.0001 * ($hour * 60 + $min); // Minute degrees
  23.  
  24. $hx = cos(deg2rad($hdeg)) * $hr; // Hour x-pos
  25. $hy = sin(deg2rad($hdeg)) * $hr; // Hour y-pos
  26.  
  27. $mx = cos(deg2rad($mdeg)) * $mr; // Minute x-pos
  28. $my = sin(deg2rad($mdeg)) * $mr; // Minuty y-pos
  29.  
  30. imagesetthickness($img, 5);
  31. imageline($img, $dx, $dy, $dx + $hx, $dy - $hy, $white); // Draw hour line
  32. imagesetthickness($img, 2);
  33. imageline($img, $dx, $dy, $dx + $mx, $dy - $my, $white); // Draw minute line
  34. imagesetthickness($img, 1);
  35.  
  36. for($i = 0; $i < 60; $i++) { // Every minute (5th degree)
  37.   $ld = 90 - 6.0001 * $i;
  38.   $lx = cos(deg2rad($ld));
  39.   $ly = sin(deg2rad($ld));
  40.  
  41.   imageline($img, $dx + $lx * 140, //x1
  42.                   $dy - $ly * 140, //y1
  43.                   $dx + $lx * 150, //x2
  44.                   $dy - $ly * 150, //y2
  45.                   $gray);
  46.  
  47.   if($i % 5 == 0) { // Every hour
  48.   imagesetthickness($img, 3);
  49.   imageline($img, $dx + $lx * 130, //x1
  50.                   $dy - $ly * 130, //y1
  51.                   $dx + $lx * 150, //x2
  52.                   $dy - $ly * 150, //y2
  53.                   $red);
  54.   imagesetthickness($img, 1);
  55.  
  56.   $x = ($dx + $lx * 165);
  57.   $y = ($dy - $ly * 165);
  58.  
  59.   imagestring($img, 3, $x - 3, $y - 5, preg_replace("/^0$/", "12", $i / 5), $gray);
  60.   }
  61.  
  62. }
  63.  
  64. imageellipse($img, $dx, $dy, 300, 300, $white);
  65.  
  66. imagestring($img, 5, $width / 2 - 20, 20, "Kello", $white);
  67. imagestring($img, 5, $width / 2 - 20, 35, date("H:i", time() + 3600), $white);
  68.  
  69. header("Content-type: image/png");
  70. imagepng($img);
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment