<?php
/*******************************************
Database query to get poll info
*******************************************/
connect();
$query = "select * from site_poll order by poll_number DESC";
$result = mysql_query($query);
$num_candidates = mysql_num_rows($result);
// calculate total number of votes so far
$total_votes=0;
while ($row = mysql_fetch_array($result))
{
$total_votes += $row ['poll_number'];
}
@mysql_data_seek($result, 0); // reset result pointer
/*******************************************
Initial calculations for graph
*******************************************/
// set up constants
putenv('GDFONTPATH=');
$width=400; // width of image in pixels - this will fit in 640x480
$left_margin = -30; // space to leave on left of graph
$right_margin= 80; // ditto right
$bar_height = 15;
$bar_spacing = 10;
$font = 'arial.ttf';
$title_size= 14; // point
$main_size= 9; // point
$darsad_size= 8; // point
$small_size= 9; // point
$text_indent = 10; // position for text labels from edge of image
// set up initial point to draw from
$x = $left_margin + 60; // place to draw baseline of the graph
$y = 50; // ditto
$bar_unit = ($width-($x+$right_margin)) / 100; // one "point" on the graph
// calculate height of graph - bars plus gaps plus some margin
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;
/*******************************************
Set up base image
*******************************************/
// create a blank canvas
$im = ImageCreateTrueColor($width,$height);
// Allocate colors
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,150,150,150);
$black=ImageColorAllocate($im,0,0,0);
$cry=ImageColorAllocate($im,200,200,200);
$pink = ImageColorAllocate($im,0,128,128);
$yellow = ImageColorAllocate($im,244,0,0);
$text_color = $black;
$percent_color = $white;
$percent_color2 = $black;
$bg_color = $white;
$line_color = $cry;
$bar_color = $blue;
$number_color = $pink;
// Create "canvas" to draw on
ImageFilledRectangle($im,0,0,$width,$height,$bg_color);
// Draw outline around canvas
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
// Add title
$title = 'Poll Results';
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // center it in x
$title_y = ($y - $title_height)/2 + $title_above_line; // center in y gap
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);
// Draw a base line from a little above first bar location
// to a little below last
ImageLine($im, $x, $y-2, $x, $height-15, $line_color);
/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
$si = 1 ;
while ($row = mysql_fetch_array ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row ['poll_number'] / $total_votes )*100));
else
$percent = 0;
// display percent for this value
$percent_dimensions = ImageTTFBBox($main_size, 0, $font, $percent. ' % ');
$percent_length = $percent_dimensions[2] - $percent_dimensions[0];
if ($total_votes > 0)
$right_value = intval(round(($row ['poll_number']/$total_votes)*100));
else
$right_value = 0;
// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);
// draw bar for this value
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
// draw title for this value
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/1.5),
$text_color, $font, $si);
// draw outline showing 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);
// display numbers
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)+20, $y+($bar_height/1.5),
$number_color, $font, $row ['poll_number'] .' - '.$total_votes);
//*********************************************
if ($percent <= 93)
{
ImageTTFText($im, $darsad_size, 0, $width-$percent_length-80,
$y+($bar_height/1.5), $percent_color2, $font, $percent .' % ');
}
if ($percent >= 90 )
{
ImageTTFText($im, $darsad_size, 0, $width-$percent_length-80,
$y+($bar_height/1.5), $percent_color, $font, $percent.' % ');
}
// move down to next bar
$y=$y+($bar_height+$bar_spacing);
$si++ ;
}
/*******************************************
Display image
*******************************************/
Imagejpeg($im,"style/images/poll.jpg",100);
/*******************************************
Clean up
*******************************************/
ImageDestroy($im);
?>