To load this file without formatting, visit http://www.mariospada.net/files/analogclock/analogclock.txt. This is a spam-protection measure; sorry for the inconvenience.

· analogclock.php ·

   1<?php
    /**
     *  Class to display an analog clock.
     *
   5 *  Requirement: 
     *    - PHP 5.2.x or above
     *    - GD  2.0.1 or above  
     *  
     *  Some of the highlights:
  10 *
     *      -   Autoscale depending on width (min 30px) 
     *      -   set quadrant color background
     *      -   set quadrant color foreground
     *      -   set image color background
  15 *      -   set color of seconds hand  
     *      -   set color of minutes hand
     *      -   set color of hours hand              
     *      -   set autotime (server-side mode)
     *      -   Choose to display or not seconds
  20 *
     *  This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivs 2.5 License.
     *  To view a copy of this license, visit {@link http://creativecommons.org/licenses/by/2.5/it/} 
     *  For more resources visit {@link http://www.mariospada.net}
     *
  25 *  @name       Analogclock
     *  @package    Analogclock
     *  @version    1.0.0 alfa (last revision: July 06, 2008)
     *  @author     Mario Spada <znevb at znevbfcnqn.arg>
     *  @copyright  (c) 2008 Mario Spada
  30 *  @example    test.php
     */
    class Analogclock 
    {
      public $clockWidth;
  35  protected $img;
      protected $shiftAngle;
      protected $r;
      protected $colorBackgr;
      protected $imgBackgr;
  40  protected $colorForegr;
      protected $colorText;
      protected $colorMin;
      protected $colorHour;
      protected $colorSec;
  45  protected $displayText = true;
      protected $textSize = 5;
      protected $autotime = false;
      protected $showSeconds = true;
      
  50  public function __construct($clockWidth=200)
      {
        $this->clockWidth = $clockWidth;
        if ($this->clockWidth < 30 ) {
          $this->clockWidth = 30;
  55    }
        if ($this->clockWidth < 90 ) {
          $this->displayText = false;
        }
        if ($this->clockWidth >= 90 && $this->clockWidth < 150) {
  60      $this->textSize = 4;
        }
    
        $this->img = @imagecreatetruecolor($clockWidth, $clockWidth)
                or die("Cannot Initialize new GD image stream"); 
  65    $this->shiftAngle = M_PI + M_PI / 2 ;
        $this->r = ($this->clockWidth) / 2;
    
        $this->colorBackgr = imageColorAllocate($this->img, 255, 255, 255);
        $this->imgBackgr = imageColorAllocate($this->img, 255, 255, 255);
  70    $this->colorForegr = imageColorAllocate($this->img, 0, 0, 0);
        $this->colorText   = imageColorAllocate($this->img, 0, 255, 0);
        $this->colorMin    = imageColorAllocate($this->img, 0, 0, 0);
        $this->colorHour   = imageColorAllocate($this->img, 0, 0, 0);
        $this->colorSec    = imageColorAllocate($this->img, 255, 0, 0);
  75  }
    
      public function setColorBackground($red, $green, $blue)
      {
        $this->colorBackgr = imageColorAllocate($this->img, $red, $green, $blue);
  80  }
      public function setImgBackground($red, $green, $blue)
      {
        $this->imgBackgr = imageColorAllocate($this->img, $red, $green, $blue);
      }
  85  public function setColorForeground($red, $green, $blue)
      {
        $this->colorForegr = imageColorAllocate($this->img, $red, $green, $blue);
      }
      public function setColorMin($red, $green, $blue)
  90  {
        $this->colorMin = imageColorAllocate($this->img, $red, $green, $blue);
      }
      public function setColorHour($red, $green, $blue)
      {
  95    $this->colorHour = imageColorAllocate($this->img, $red, $green, $blue);
      }
      public function setColorSec($red, $green, $blue)
      {
        $this->colorHour = imageColorAllocate($this->img, $red, $green, $blue);
 100  }
      public function setAutotime($value)
      {
        $this->autotime = $value;
      }
 105  public function showSeconds($value)
      {
        $this->showSeconds = $value;
      }
      
 110  private function drawBackground()
      {
        imagefill($this->img, 0, 0, $this->imgBackgr);
        imageantialias($this->img, true );
    
 115    imagefilledellipse  ($this->img, $this->clockWidth/2, $this->clockWidth/2, 
                             $this->r, $this->r, $this->colorBackgr );    
      }
    
      private function imagelinethick($x1, $y1, $x2, $y2, $color, $thick = 1)
 120  {
        if ($thick == 1) {
            return imageline($this->img, $x1, $y1, $x2, $y2, $color);
        }
        $t = $thick / 2 - 0.5;
 125    if ($x1 == $x2 || $y1 == $y2) {
            return imagefilledrectangle($this->img, round(min($x1, $x2) - $t), 
                                                round(min($y1, $y2) - $t), 
                                                round(max($x1, $x2) + $t), 
                                                round(max($y1, $y2) + $t), 
 130                                            $color);
        }
        $k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
        $a = $t / sqrt(1 + pow($k, 2));
        $points = array(
 135        round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
            round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
            round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
            round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
        );
 140    imagefilledpolygon($this->img, $points, 4, $color);
        return imagepolygon($this->img, $points, 4, $color);
      }
    
      private function drawQuadrant()
 145  {
        $gamma = $this->shiftAngle ;
        
        for ($i = 1; $i <= 12; $i++)
        {
 150      $gamma += ((M_PI * 1 / 6) * $i) ;
          $X = $this->r + (cos($gamma) * $this->r * 0.9);
          $Y = $this->r + (sin($gamma) * $this->r * 0.9);
          $X1 = $this->r + (cos($gamma) * $this->r);
          $Y1 = $this->r + (sin($gamma) * $this->r);
 155      $thickness = ($i == 3 || $i == 6 || $i == 9 || $i == 12) ? 4 : 1; 
          $this->imagelinethick($X, $Y, $X1, $Y1, $this->colorForegr, $thickness);
          $gamma = $this->shiftAngle ;
        }
      }
 160
      public function display($hour=0, $minutes=0, $seconds=0)
      {
        $this->drawBackground();
        $this->drawQuadrant();
 165    
        if($this->autotime)
        {
          $today = getdate();
          $minutes = $today['minutes'];
 170      $hour    = $today['hours'];
          $seconds = $today['seconds']; 
        }
        
        if ($this->showSeconds)
 175    {
          $timeText = sprintf("%02d:%02d:%02d",$hour,$minutes,$seconds);
          $xImgShift = 30;
        }
        else
 180    {
          $timeText = sprintf("%02d:%02d",$hour,$minutes);
          $xImgShift = 20;
        }
       
 185    if ($this->displayText)
        {
          imageString($this->img, $this->textSize, 
                      $this->r-$xImgShift, $this->r+10, $timeText, $this->colorText);
        }
 190    
        $alfa = $beta = $delta = $this->shiftAngle ;
        
        $delta += (M_PI * 1 / 30) * $seconds ;
        $alfa += (M_PI * 1 / 30) * $minutes ;
 195    $beta += (M_PI * 1 / 6) * ($hour  + $minutes/60) ;
        // seconds
        $newX0 = $this->r + cos($delta) * ($this->r - 5);
        $newY0 = $this->r + sin($delta) * ($this->r - 5) ;
        // minutes
 200    $newX1 = $this->r + cos($alfa) * ($this->r * 3 / 4) ;
        $newY1 = $this->r + sin($alfa) * ($this->r * 3 / 4) ;
        // hours
        $newX2 = $this->r + cos($beta) * ($this->r *  2 / 3) ;
        $newY2 = $this->r + sin($beta) * ($this->r * 2 / 3) ;
 205    
        if ($this->showSeconds)
        {
          imageLine($this->img, $this->r, $this->r, $newX0, $newY0, $this->colorSec);
        }
 210    $this->imagelinethick($this->r, $this->r, $newX1, $newY1, $this->colorMin, 2);
        $this->imagelinethick($this->r, $this->r, $newX2, $newY2, $this->colorHour, 3);
                                   
        // set the content type
        //header("Content-type:  image/gif");
 215    header("Content-type:  image/png");
        // create an interlaced image for better loading in the browser
        imageInterlace($this->img, 1);
        // now send the picture to the client (this outputs all image data directly)
        imagePNG($this->img);
 220  }
    }
    ?>
    

· analogclock.php ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.