Source for file AnalogClock.class.php

Documentation is available at AnalogClock.class.php

  1. <?php 
  2. /**
  3.  * @author Takács Ákos (Rimelek), programmer [at] rimelek [dot] hu
  4.  * @copyright Copyright (C) 2009, Takács Ákos
  5.  * @package REPHPGraph
  6.  */
  7.  
  8. /**
  9.  * Aobject importálása
  10.  */
  11. require_once dirname(__FILE__)."/../AObject.class.php";
  12.  
  13. /**
  14.  * IObject importálása
  15.  */
  16. require_once dirname(__FILE__)."/../interfaces/IObject.class.php";
  17.  
  18. /**
  19.  * Ellipszis iterátor importálása
  20.  */
  21. require_once dirname(__FILE__)."/../iterators/MultiEllipseIterator.class.php";
  22.  
  23. /**
  24.  * Analóg óra
  25.  *
  26.  * Egy analóg órát lehet létrehozni vele. A kinézetét a többi objektumhoz hasonlóan módosítani lehet.
  27.  *
  28.  * {@example ../examples/analogclock.php}
  29.  *
  30.  * @property $hourHandColor 
  31.  * @property $minuteHandColor 
  32.  * @property $secondHandColor 
  33.  * @property $segmentationColor 
  34.  *
  35.  * @version 1.0
  36.  * @package REPHPGraph
  37.  */
  38. class AnalogClock extends AObject implements IObject
  39. {
  40.     /**
  41.      * A megjelenített időpont elemei
  42.      * 
  43.      * <b>indexek:<b><br />
  44.      * <ul>
  45.      *     <li>hour12: Az óra 12 órás formátumban</li>
  46.      *     <li>hour24: Az óra 24 órás formátumban</li>
  47.      *    <li>minute: perc</li>
  48.      *     <li>second: másodperc</li>
  49.      *     <li>am_pm: Délelőtt, vagy délután</li>
  50.      * </ul>
  51.      *
  52.      * @var array 
  53.      */
  54.     protected $time = array();
  55.     
  56.     /**
  57.      * A használt színek
  58.      *
  59.      * @var array 
  60.      */
  61.     protected $colors = array();
  62.     
  63.     /**
  64.      * Az óra sugara
  65.      *
  66.      * @var int 
  67.      */
  68.     protected $radius=0;
  69.  
  70.     /**
  71.      * Óramutató aránya a sugárhoz képest százalékban
  72.      *
  73.      * @var int 
  74.      */
  75.     protected $hourHandRatio=50;
  76.     
  77.     /**
  78.      * Percmutató aránya a sugárhoz képest százalékban
  79.      *
  80.      * @var int 
  81.      */
  82.     protected $minuteHandRatio=70;
  83.     
  84.     /**
  85.      * Másodpercmutató aránya a sugárhoz képest százalékban
  86.      *
  87.      * @var int 
  88.      */
  89.     protected $secondHandRatio=80;
  90.     
  91.     /**
  92.      * Óra létrehozása
  93.      *
  94.      * @param int $radius Az óra sugara
  95.      * @param int $time A megjelenítendő időpont (timestamp). Elhagyása esetén az aktuális idő
  96.      */
  97.     function __construct($radius,$time=null)
  98.     {
  99.         $this->width = $this->height = ($this->radius = $radius)*2;
  100.         $time ($time !== null$time time()
  101.         
  102.         //színek beállítása
  103.         $this->time['hour12'date("h",$time);
  104.         $this->time['hour24'date("H",$time);
  105.         $this->time['minute']  date("i",$time);
  106.         $this->time['second'date("s",$time);
  107.         $this->time['am_pm'date("A",$time);
  108.  
  109.         $this->colors['borderColor']        array(0,0,1);            //óralap szegélye
  110.         $this->colors['color']            array(255,255,254);        //óralap színe
  111.         $this->colors['backGround']        array(255,255,255);        //háttér
  112.         $this->colors['hourHandColor']        array(200,10,60);        //óra mutató színe
  113.         $this->colors['minuteHandColor']    array(100,200,250);        //perc mutató színe
  114.         $this->colors['secondHandColor']    array(20,200,30);        //másodperc mutató színe
  115.         $this->colors['segmentationColor']    array(0,0,0);            //Az órán a beosztások, és a középont színe
  116.         //kép létrehozása     
  117.         $this->source = imagecreatetruecolor($this->width,$this->height);
  118.         $this->setColors();
  119.     }
  120.     
  121.     /**
  122.      * Tulajdonságok lekérdezésének szabályozása
  123.      *
  124.      * @param string $var 
  125.      * @return mixed 
  126.      */
  127.     public function __get($var
  128.     {
  129.         if (isset($this->colors[$var])) return $this->colors[$var];
  130.         return parent::__get($var);
  131.     }
  132.     
  133.     /**
  134.      * Tulajdonságok beállításának szabályozása
  135.      * 
  136.      * @param string $var 
  137.      * @param mixed $value 
  138.      */
  139.     public function __set($var,$value
  140.     {
  141.         if (isset($this->colors[$var])) $this->colors[$var$value;
  142.         else parent::__set($var,$value);
  143.     }
  144.     
  145.     /**
  146.      * Színek beállítása
  147.      *
  148.      */
  149.     protected function setColors()
  150.     {
  151.         foreach($this->colors as $key => $value)
  152.         
  153.             $this->$key $this->createColor($value[0],$value[1],$value[2]);
  154.         }    
  155.     }
  156.  
  157.     /**
  158.      * Az óralap nem mozgó elemeinek kirajzolása
  159.      *
  160.      */
  161.     protected function dial()
  162.     {
  163.         //Az óralapot határoló kör létrehozása
  164.         ImageArc($this->source,
  165.             round($this->width/2),round($this->height/2),
  166.             $this->width-1,$this->height-1,0,360,$this->borderColor);
  167.         //Az óra, és perc beosztások kirajzolása
  168.         $this->segmentation();
  169.         //A 'délután' (PM), vagy 'délelőtt' (AM) jelzése
  170.         ImageString($this->source,2,round($this->width/2)-3,round($this->height/4),$this->time['am_pm'],$this->segmentationColor);
  171.     }
  172.  
  173.     /**
  174.      * Óra-, és percosztások kirajzolása
  175.      *
  176.      */
  177.     protected function segmentation()
  178.     {
  179.         //4 elipszis ( tömött karikák,vonalak belső vége, vonalak külső vége, számok )
  180.         // középpontjának x, és y koordinátája 
  181.         $cx array($this->width/,$this->width/2,$this->width/,$this->width/2);
  182.         $cy array($this->height/2,$this->height/2,$this->height/2,$this->height/2);
  183.         //a fenti 4 elipszis szélessége , és magassága
  184.         $width array($this->width-10,$this->width-4,$this->width-20,$this->width-34);
  185.         $height array($this->height-10,$this->height-4,$this->height-20,$this->height-34);
  186.         
  187.         //Iterátor létrehozása több elipszis koordinátáival
  188.         $iterator new MultiEllipseIterator($cx,$cy,$width,$height);
  189.         $iterator->start = -60//iteráció kezdete fokban
  190.         $iterator->end 299//iteráció vége
  191.         $iterator->add 6//Lépésköz
  192.  
  193.         //Az óralap
  194.         $hour 1;
  195.         foreach($iterator as $k=>$position
  196.             //A perc osztások 
  197.             if(($iterator->_i 30)){
  198.                 ImageFilledArc($this->sourceround($position['x'][0])round($position['y'][0])3,30360$this->segmentationColorIMG_ARC_PIE);
  199.             //Az óra osztások
  200.             }else
  201.                 imageLine($this->sourceround($position['x'][1])round($position['y'][1])round($position['x'][2])round($position['y'][2])$this->segmentationColor);
  202.                 //Az óra osztások mellett az óra száma
  203.                 ImageString($this->source,2,round($position['x'][3])-3,round($position['y'][3])-5,$hour,$this->segmentationColor);
  204.                 $hour++
  205.             }
  206.         }
  207.     }
  208.  
  209.     /**
  210.      * Mutatók kirajzolása
  211.      */
  212.     protected function hands()
  213.     {
  214.         $this->hourHand();    //óra
  215.         $this->minuteHand();    //perc
  216.         $this->secondHand();    //másodperc
  217.     }
  218.  
  219.     /**
  220.      * Óramutató létrehozása
  221.      *
  222.      * @param double $arany Óramutató aránya a sugárhoz képest
  223.      */
  224.     protected function hourHand()
  225.     {
  226.         $ratio $this->hourHandRatio/100;
  227.         //óra mutató poziciója
  228.         $hourDeg deg2rad30 $this->time['hour12'deg2rad(90deg2rad($this->time['minute']/2);
  229.         $hourX (($this->radius*$ratiocos($hourDeg)) $this->radius;
  230.         $hourY (($this->radius*$ratiosin($hourDeg)) $this->radius;
  231.  
  232.         //óra mutató
  233.         imageLine($this->sourceround($this->width/2)round($this->height/2)round($hourX)round($hourY)$this->hourHandColor);
  234.     }
  235.  
  236.     /**
  237.      * Percmutató létrehozása
  238.      *
  239.      * @param double $arany Percmutató aránya a sugárhoz képest
  240.      */    
  241.     protected function minuteHand()
  242.     {
  243.         $ratio $this->minuteHandRatio/100;
  244.         //a perc mutató poziciója
  245.         $minuteDeg deg2rad$this->time['minute'deg2rad(90deg2rad($this->time['second']/10);
  246.         $minuteX (($this->radius*$ratiocos($minuteDeg)) $this->radius;
  247.         $minuteY (($this->radius*$ratiosin($minuteDeg)) $this->radius;
  248.         
  249.         //perc mutató
  250.         imageLine($this->sourceround($this->width/2)round($this->height/2)round($minuteX)round($minuteY)$this->minuteHandColor);
  251.     }
  252.     
  253.     /**
  254.      * Másodpercmutató létrehozása
  255.      *
  256.      * @param double $arany Másodpercmutató aránya a sugárhoz képest
  257.      */    
  258.     protected function secondHand()
  259.     {
  260.         $ratio $this->secondHandRatio/100;
  261.         //a másodperc poziciójának számítása
  262.         $secondDeg deg2rad$this->time['second'deg2rad(90);
  263.         $secondX (($this->radius*$ratiocos($secondDeg)) $this->radius;
  264.         $secondY (($this->radius*$ratiosin($secondDeg)) $this->radius;
  265.  
  266.         //Másodperc mutató
  267.         imageLine($this->sourceround($this->width/2)round($this->height/2)round($secondX)round($secondY)$this->secondHandColor);    
  268.     }
  269.     
  270.     /**
  271.      * A teljes óra kép létrehozáa
  272.      *
  273.      */
  274.     public function create()
  275.     {
  276.         //egyező háttér és szegélyszín esetén új keretszín választása
  277.         $color $this->createColor(1,1,1);
  278.         if ($this->borderColor == $this->backGround{
  279.             if ($this->borderColor == $color{
  280.                 $this->borderColor = $this->createColor(0,0,0);
  281.             else {
  282.                 $this->borderColor = $color;
  283.             }
  284.         }
  285.  
  286.         //óralap színének beállítása
  287.         imagefill($this->source,0,0,$this->color);
  288.         //Óralap kirajzolása
  289.         $this->dial();
  290.         //Mutatók kirajzolása
  291.         $this->hands();
  292.         //háttér beállítás
  293.         imagefill($this->source,0,0,$this->backGround);
  294.         imagefill($this->source,0,$this->height-1,$this->backGround);
  295.         imagefill($this->source,$this->width-1,$this->height-1,$this->backGround);
  296.         imagefill($this->source,$this->width-1,0,$this->backGround);
  297.         //a középpontban egy tömött karika
  298.         ImageFilledArc($this->sourceround($this->width/2)round($this->height/2)5,50360$this->segmentationColorIMG_ARC_PIE);
  299.     }
  300.     
  301. }
  302.  
  303. ?>

Documentation generated on Tue, 09 Feb 2010 01:17:46 +0100 by phpDocumentor 1.4.1