PDA

View Full Version : Creating an image with a random color


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.

mouse
05-04-2003, 08:15 AM
<?
$R = rand(0,255);
$G = rand(0,255);
$B = rand(0,255);

$width = $_GET["percent"];
$width = round($width);

$height = "8";

header ("Content-type: image/png");
$im = @imagecreate ($width, $height) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, $R, $G, $B);
imagepng ($im);
imagedestroy($im);
?>

Ökii
05-04-2003, 08:38 AM
as mouse showed, imagecolorallocate cannot just accept a single variable array name for the 3 colour parameters

maybe

$colors = array(array('0,0,255'),array('0,255,0'),array('255,0,0'),array('255,255,0'),array('2
55,0,255'),array('0,255,255'));
....
$rand_colour = imagecolorallocate($image,$color[$num][0],$color[$num][1],$color[$num][2]);
imagefill($image, 0, 0, $rand_colour);

lose the for ($i .) loop though as you only need define one colour

pardicity3
05-04-2003, 06:01 PM
Very exciting. Both very good solutions...I shall try them at a later time but they look as though they should work just fine. Although I think I may end up going with Okii's not because it is better but rather it won't put out funky colors like those that mouse's might. But I shall see, thanks much :thumbsup:

Oh, and I figured that the problem was that image color allocate couldn't take a single variable for the three colors but I thought I might as well try! Thanks though for the input.

pardicity3
05-04-2003, 09:59 PM
Follow-up:

Okay, so I ended up actually using mouse's script because I couldn't get yours to work Okii. It kept giving me black...which seemed sort of odd, but oh well.

Anyway, mouse your script is working great except for one thing. The images that have the same length are the same color. Is there something wierd happening here where rand() is seeding based on my $width variable or, since width is declared afterwards, my $_GET["percent"] query string?!

Here is a link to the results page (it is in bad shape right now, but it works :)):
http://www.mikesadventures.net/polls/results.php
http://www.mikesadventures.net/polls/vote.php - if you want to vote you can go here...I just felt like putting this in here :p

Here is the code. It's basically the same as yours, but I added just a tad to get it the way I wanted:


//color stuff
$R = rand(0,255);
$G = rand(0,255);
$B = rand(0,255);
//make sure it's not too close to black...
if ($R<100 && $G<100 && $B<100) {
$R = $R + 100;
$G = $G + 100;
$B = $B + 100;
}
//make sure it's not too close to white...
if ($R>220 && $G>220 && $B>220) {
$R = $R - 100;
$G = $G - 100;
$B = $B - 100;
}

//size attributes
$width = $_GET["percent"];
$width = round($width);
if ($width < 1) {
$width = 1;
}
$width = $width *4;
$height = "8";

//image stuff
header ("Content-type: image/png");
$im = @imagecreate ($width, $height) or die ("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate ($im, $R, $G, $B);
imagepng ($im);
imagedestroy($im);

mouse
05-04-2003, 10:20 PM
Because the images are the same and so the browser is using the same image repeatedly, any image with 12.5% of the vote is pollimage.php?percent=12.5, you need to give each an id of some sort, so it's:
pollimage.php?percent=12.5&id=1
pollimage.php?percent=12.5&id=2
pollimage.php?percent=12.5&id=3
etc

Ökii
05-05-2003, 07:54 AM
the reason my quickly flytyped version wouldn't do much is the single quotes around the array bits

=array(array('......') <-- one var
=array(array( , , ) <-- 3

suggestion... write a function that can take all the poll values and output the entire graph based on those values

pardicity3
05-05-2003, 10:01 PM
Okii - you mean just have one big image? That was my next goal...but one question on that. Can I create an image with a transparent background? Otherwise it might take me a little more work to get my graph to look nice with all the different backgrounds you can have on me site.


I would especially like to look into this as the results I am getting now could easily--more easily in fact--be done by just stretching a pre-made image to a specific height instead of making a completely new image each time. Right now my method seems like the long route/memory hog choice :(

Ökii
05-06-2003, 09:19 AM
Transparency in GD is a bit hit or miss at the moment as the filetype would have to be either .gif (which it cannot write since 1.6 or so) or .png which doesn't render in IE very well.

You only really be able to do a graph function either drawn over a background image (which would mean your width/height being defined) or on a solid or gradating background colour or tile.

mouse
05-06-2003, 09:54 AM
It's possible to force IE into using the transparency, it's detailed in this thread: http://codingforums.com/showthread.php?s=&threadid=15414&highlight=png%2A%2A