Php/GdClock

From Php-Classes

Jump to: navigation, search

[edit] an Ajax clock using GD

  • this is a simple class using GD graphic library
  • you need to copy an rgb.txt file from linux system to make this script work. Just copy to current directory.
  • demo and files : http://php.classes.free.fr/php/gd/
  • there is some problems rendering this class source in wiki format. Don't know how to achieve.

[edit] Source Code

<?php

 class ColorRGB{
 	protected $colors;
 
 	public function __construct($file='rgb.txt'){
 		if(!file_exists($file))
 			throw new Exception("RGB file not found ($file)");
 
 		$this->load_rgb_file($file);
 	}
 
 	protected function load_rgb_file($file){
 		$lines = split("\n", file_get_contents($file));
 
 		$this->colors = array();
 		foreach($lines as $line){
 			if($line == )
 				continue;
 			if($line[0] == '!')
 				continue;
 			$list = preg_split('/\s+/', $line);
 
 			$name = strtolower($list[3]);
 			$this->colors[$name] = array($list[0], $list[1], $list[2]);
 		}
 	}
 
 	public function get($name){
 		if(isset($this->colors[$name]))
 			return $this->colors[$name];
 		return false;
 	}
 }
 
 class GdImage{
 
 	protected $w;
 	protected $h;
 
 	protected $img;
 	protected $colors;
 	protected $colorsRGB;
 
 	public function __construct($w=500, $h=500){
 		$this->w = $w;
 		$this->h = $h;
 
 		$this->colorsRGB = new ColorRGB();
 		$this->create();
 	}
 
 	public function create(){
 		$this->img = imagecreatetruecolor($this->w, $this->h);
 		$this->filledRectangle(0, 0, $this->w, $this->h, $this->color(255,255,255));
 		$this->Rectangle(0, 0, $this->w-1, $this->h-1, $this->color(0,0,0));
 	}
 
 	public function display(){
 
 		if(!headers_sent($file, $line)){
 			header ("Content-type: image/png");
 			$this->header_no_cache();
 			imagepng($this->img);
 		}
 		else
 			echo "erreur ; affichage image impossible !\n";
 
 	}
 
 	public function filledRectangle($x, $y, $w, $h, $color){
 		imageFilledRectangle($this->img, $x, $y, $w, $h, $color);
 	}
 
 	public function rectangle($x, $y, $w, $h, $color){
 		imageRectangle($this->img, $x, $y, $w, $h, $color);
 	}
 
 	public function filledCircle($x, $y, $w, $color){
 		imageFilledEllipse($this->img, $x, $y, $w, $w, $color);
 	}
 
 	public function line($x1, $y1, $x2, $y2, $color){
 		imageLine($this->img, $x1, $y1, $x2, $y2, $color);
 	}
 
 	public function color($r,$v,$b, $alpha=null){
 		if($alpha == null)
 			return imagecolorallocate($this->img, $r, $v, $b);
 		else
 			return imagecolorallocatealpha($this->img, $r, $v, $b, $alpha);
 
 	}
 
 	public function line_width($w){
 		imagesetthickness($this->img, $w);
 	}
 
 	public function set_antialias($bool=true){
 		if(function_exists('imageantialias'))
 			imageantialias($this->img, $bool);
 	}
 
 	public function header_no_cache(){
 		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
 		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
 	}
 
 	public function color_named($name, $alpha=null){
 		$rgb = $this->colorsRGB->get($name);
 		if($rgb == false)
 			return $this->color(0,0,0,$alpha);
 		return $this->color($rgb[0], $rgb[1], $rgb[2] ,$alpha);
 	}
 
 	public function get_arg($name, $default=false){
 		if(isset($_GET[$name]))
 			return $_GET[$name];
 		return $default;
 	}
 }
 
 class Clock extends GdImage{
 
 	protected $width;
 	protected $center;
 	protected $border;
 	protected $radius;
 
 	public function __construct($width, $border=10){
 		parent::__construct($width, $width);
 
 		$this->set_antialias();
 
 		$this->width = $width;
 		$this->center = intval($width/2);
 		$this->border = $border;
 		$this->radius = $this->center;
 
 		$this->draw();
 	}
 
 	public function draw(){
 
 		$this->filledRectangle(
 			1, 1, $this->width - 1, $this->width - 1,
 			$color=$this->color_named('lightblue', $alpha=80));
 
 
 		$this->filledCircle(
 			$this->center, $this->center, 
 			$this->width-2*$this->border, 
 			$color=$this->color_named('pink', $alpha=50));
 
 		$color = $this->color_named('black', 50);
 
 		$this->line_width(1);
 		for($i=0 ; $i<60 ; $i++)
 			$this->draw_ticks($this->radius - $this->border, 3, $i*6, $color);
 
 		$this->line_width(2);
 		for($i=0 ; $i<12 ; $i++)
 			$this->draw_ticks($this->radius - $this->border, 8, $i*30, $color);
 
 		$color = $this->color_named('black', 40);
 
 		# seconds :
 		$time = time();
 		$sec = $time % 60;
 		$min = date('i', $time);
 		$hour = date('h', $time);
 
 		# seconds
 		$angle = intval(6 * ($sec));
 		$this->line_width(1);
 		$this->draw_hand_line(intval($this->radius * 0.85), $angle, $color);
 
 		# minutes :
 		$angle = intval(6 * ($min));
 		$this->line_width(2);
 		$this->draw_hand_line(intval($this->radius * 0.70), $angle, $color);
 
 		# hours :
 		$angle = intval($hour * 360/12 + $min * 12/30);
 		$this->line_width(3);
 		$this->draw_hand_line(intval($this->radius * 0.50), $angle, $color);
 	}
 
 	protected function draw_hand_line($radius, $angle, $color){
 		$angle = (180 - $angle) * pi() / 180;
 
 		$x1 = $y1 = $this->center;
 		$x2 = intval($x1 + sin($angle) * $radius);
 		$y2 = intval($y1 + cos($angle) * $radius);
 		$this->line($x1, $y1, $x2, $y2, $color);
 	}
 
 	protected function draw_ticks($radius, $size, $angle, $color){
 		$angle = (180 - $angle) * pi() / 180;
 
 		$x1 = $y1 = $this->center;
 
 		$x2 = intval($x1 + sin($angle) * $radius);
 		$y2 = intval($y1 + cos($angle) * $radius);
 
 		$x1 = intval($x1 + sin($angle) * ($radius-$size));
 		$y1 = intval($y1 + cos($angle) * ($radius-$size));
 
 		$this->line($x1, $y1, $x2, $y2, $color);
 	}
 
 	public function display(){
 		if($this->get_arg('action') == 'display')
 			parent::display();
 		else
 			$this->html();
 
 	}
 
 	public function html(){
 
 	echo <<<END
 <html>
 <script> 
 var clock; 
 var imgBase="?action=display&count=" 
 var c = 0; 
 
 function count() 
 {
  c++;
 
  webcamimage = document.getElementById("clock");
  webcamimage.src=imgBase + c;
 
 }
 
 function init() 
 { 
  webcamimage = document.getElementById("clock"); 
  if( webcamimage ) 
  { 
   setInterval("count()",1000); 
  } 
 } 
 window.onload = init; 
 </script> 
 <body> 

Ajax Clock with GD
 
<img src="clock.php" name="image" id="clock">

 </body> 
 </html>
 
 END;
 
 	}
 }
 
 $clock = new Clock(200);
 $clock->display();
 ?>

[edit] Links

Personal tools
Languages