Thursday, January 2, 2014

Sample PHP Codes

Below you may find some sample PHP Codes :



Chart Demo : 

<?php
$myImage = ImageCreate(300,300);

$white = ImageColorAllocate ($myImage, 255255255);
$red  = ImageColorAllocate ($myImage, 25500);
$green = ImageColorAllocate ($myImage, 02550);
$blue = ImageColorAllocate ($myImage, 00255);
$lt_red = ImageColorAllocate($myImage, 255150150);
$lt_green = ImageColorAllocate($myImage, 150255150);
$lt_blue = ImageColorAllocate($myImage, 150150255);

for ($i = 120;$i > 100;$i--) {
ImageFilledArc ($myImage, 100, $i, 200150090, $lt_red, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 20015090180, $lt_green, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 200150180360, $lt_blue, IMG_ARC_PIE);
}

ImageFilledArc($myImage, 100100200150090, $red, IMG_ARC_PIE);
ImageFilledArc($myImage, 10010020015090180 , $green, IMG_ARC_PIE);
ImageFilledArc($myImage, 100100200150180360 , $blue, IMG_ARC_PIE);

header ("Content-type: image/png");
ImagePNG($myImage);

ImageDestroy($myImage);
?>


Pattern Demo : 

<?php

class ClassFactory{
   private $registeredClasses = array();
   static private $instance = NULL;

   private function __construct() {}

   static function getInstance(){
      if(self::$instance == NULL){
         self::$instance = new ClassFactory();
      }
      return self::$instance;
   }

   function registerClass($id, $creator_func){
      $this->registeredClasses[$id= $creator_func;
   }

   function createObject($id, $args) {
     if(!isset($this->registeredClasses[$id])){
        return(NULL);
     }
     return($this->registeredClasses[$id]($args));
   }
}

function MyClassCreator(){
   return "creator";
}


$factory = ClassFactory::getInstance();

$factory->registerClass(1"MyClassCreator");

$instance = $factory->createObject(1, array());

?> 

No comments:

Post a Comment