Haifa linux club:PHP lecture
Prev Next


GD example : drawing graphs


graph.jpg

<?
/* We need this in order to return a picture */
header ("Content-type: image/png");

$graph_data = array(
   
"question" => "Are you addicted to PHP ?" ,
   
"yes" => 82 ,
   
"no" => 18
    
);

/* Create the images */
$im = ImageCreate(200,150) or die("could not create image !! <br>");

/* allocate the colors */
$background_color = ImageColorAllocate ($im, 200, 200, 200);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$bar_color = ImageColorAllocate ($im, 0, 0, 0);
$bar_color_fill = ImageColorAllocate ($im, 150, 150, 255);

/* draw some text .. */
ImageFill ($im,1,1,$background_color);
ImageString ($im, 3, 5, 5,  $graph_data["question"], $text_color);
ImageString ($im, 2, 30, 130, "yes: ".$graph_data["yes"]."%" , $text_color);
ImageString ($im, 2, 110, 130, "no: ".$graph_data["no"]."%" , $text_color);

/* draw the bars */
ImageRectangle ($im, 30, 120 - $graph_data["yes"],50,120 ,$bar_color);
if(
$graph_data["yes"] > 2) {    #so we won't fill the bg of the image ...
       
ImageFill ($im,31,119,$bar_color_fill);
      }

ImageRectangle ($im, 110, 120 - $graph_data["no"],130,120 ,$bar_color);
if(
$graph_data["yes"] > 2) {
       
ImageFill ($im,111,119,$bar_color_fill);
      }
/* returm image data */
ImagePng ($im);
?>



HOME