pardicity3
05-04-2003, 07:10 AM
I have a poll that I am working on just for the fun of things. I have decided to represent the amount of votes for each option through a solid colored bar that will be created dynamically using php (why else would I have posted here :)).
Anyway, I originally had all the bars as the same color, but that was no fun. So I decided to try and make a random color for each bar. My script just does not work, and despite all the thinking I can do, I can't figure out how to make this work. I realise that my script is totally off and there is no way of making it work, but I will post it anyway so that maybe you can see the method by which I am trying to do this.
//make an array of colors to choose from
$colors = array('0,0,255','0,255,0','255,0,0','255,255,0','255,0,255','0,255,255');
$count = count($colors);
$num = rand(1,$count);
$color = array();
//get the width from the query string
$width = $_GET["percent"];
$width = round($width);
//add padding just incase there is a zero width
if ($width < 1) {
$width = 1;
}
//set png type
header("Content-type: image/png");
//set width and height and creat image
$height = 8;
$image = imagecreate($width, $height);
//Allocate some colors and create image
for ($i=0;$i<$count;$i++) {
$color[$i] = imagecolorallocate($image,$colors[$i]);
}
imagefilledrectangle($image, 0, 0, $iwidth, $height, $color[$num]);
//make image
imagepng($image);
//kill image
imagedestroy($image);
I orignally tried to make an array with rgb values. Then I would hav a loop executing the same amount of times as there are colors. Then I had a rectangle with a random color created.
I am positive that my problem lie with the fact that my imagecolorallocate should look more like this:
$color[$i] = imagecolorallocate($image,$hex1,$hex2,$hex3)
but I just can't figure out an easy way to make that work. Arr, there is probably a simple solution out there and I just can't think of it cause it is too late (or early...it's 1:10am) and I need to sleep.
Anyway, I originally had all the bars as the same color, but that was no fun. So I decided to try and make a random color for each bar. My script just does not work, and despite all the thinking I can do, I can't figure out how to make this work. I realise that my script is totally off and there is no way of making it work, but I will post it anyway so that maybe you can see the method by which I am trying to do this.
//make an array of colors to choose from
$colors = array('0,0,255','0,255,0','255,0,0','255,255,0','255,0,255','0,255,255');
$count = count($colors);
$num = rand(1,$count);
$color = array();
//get the width from the query string
$width = $_GET["percent"];
$width = round($width);
//add padding just incase there is a zero width
if ($width < 1) {
$width = 1;
}
//set png type
header("Content-type: image/png");
//set width and height and creat image
$height = 8;
$image = imagecreate($width, $height);
//Allocate some colors and create image
for ($i=0;$i<$count;$i++) {
$color[$i] = imagecolorallocate($image,$colors[$i]);
}
imagefilledrectangle($image, 0, 0, $iwidth, $height, $color[$num]);
//make image
imagepng($image);
//kill image
imagedestroy($image);
I orignally tried to make an array with rgb values. Then I would hav a loop executing the same amount of times as there are colors. Then I had a rectangle with a random color created.
I am positive that my problem lie with the fact that my imagecolorallocate should look more like this:
$color[$i] = imagecolorallocate($image,$hex1,$hex2,$hex3)
but I just can't figure out an easy way to make that work. Arr, there is probably a simple solution out there and I just can't think of it cause it is too late (or early...it's 1:10am) and I need to sleep.