|
Junior Member
Join Date: Dec 2006
Posts: 40
|
Re: Vote system with images
****THIS POST HAS BEEN EDITED BY O~SNAPPLE PLEASE USE THE CORRECT TAGS WHEN POSTING CODE****
Paste this code to your file it generates columns in different colors
PHP Code:
<?php
header("Content-type: image/jpeg");
include "config.php";
$query = "SELECT * FROM votes;";
$res = mysql_query($query);
$n = mysql_num_rows($res);
//$n=10;
$width = 10*25+220;
$height = 200+30;
$h = $height+$n*20+30;
$x0 = 0;
$y0 = 0;
$qanak = array($n);
for ($i=0; $i<$n; $i++) {
$rows = mysql_fetch_array($res);
$qanak[$i] = $rows['qanak'];
}
// drawing graphic
$im = @imagecreate($width, $h)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
$cols = array($n);
for ($i=0; $i<$n; $i++) {
$cols[$i] = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
}
// Frame st
imagerectangle($im,$x0,$y0,$width-1,$height-1,250);
// Frame end
// arancq st
imageline($im,$x0+15,$y0+15,$x0+15,$height-31,250);
imageline($im,$x0+15,$height-31,$width-1,$height-31,250);
// arancq end
for ($t=20;$t<($height-31);$t+=10)
{
imageline($im,$x0+15,$t,$width-1,$t,250);
}
$y2 = $height-32;
for ($i=0; $i<$n; $i++)
{
$x1 = ($i+1)*25;
$y1 = 200-$qanak[$i];
$x2 = $x1+20;
imagerectangle($im,$x1,$y1,$x2,$y2,125);
imagefilledrectangle($im,$x1+1,$y1+1,$x2-1,$y2-1,$cols[$i]);
imagestring($im,3,($i+1)*25,$height-20,$qanak[$i], $textcolor);
}
// drawing initials
drawInit($im,$height,$width,$cols,$n);
function drawInit($im,$height,$w,$cols,$n)
{
$h = $height+$n*30;
$hp = 20;
$wp = 60;
$query = "SELECT * FROM votes;";
$res = mysql_query($query);
$sum = 0;
$opt = array($n);
$opt_qan = array($n);
for ($i=0; $i<$n; $i++) {
$rows = mysql_fetch_array($res);
$opt[$i] = $rows['name'];
$opt_sum[$i] = $rows['qanak'];
$sum += $rows['qanak'];
}
imagerectangle($im,0,$height,$w-1,$h-1,125);
$x = 15;
$y = $height+10;
for ($i=0; $i<$n; $i++)
{
imagefilledrectangle($im,$x,$y,$x+60,$y+20,$cols[$i]);
imagestring($im,3,$x+60+20,$y,$opt[$i]." ".round($opt_sum[$i]/($sum/100),1)." %", $cols[$i]);
$y+=20;
}
}
imagejpeg($im);
imagedestroy($im);
?>
|