You will require a true colour image to allocate 24/32 bit colours:
imagecreatetruecolor().
Another thing I should note (although not on the documents), you can freely use hexadecimal notation in place of imagecolorallocate (which is backwards to me)
i.e. $black = 0, or $red = 0x00ff0000.
<?php
$my_img = imagecreatetruecolor( 256, 300 );
//demonstrate 255^3 colours
for($i = 0;$i < 256;$i++) {
for($j = 0;$j < 100;$j++) {
imagesetpixel($my_img, $i, $j, $i);
}
for($j = 100;$j < 200;$j++) {
imagesetpixel($my_img, $i, $j, $i << 8);
}
for($j = 200;$j < 300;$j++) {
imagesetpixel($my_img, $i, $j, $i << 16);
}
}
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>
The format is of course 0xaarrggbb and it is faster to shift or use mt_rand(0, pow(2, 24)) than to use color allocation(rand, rand, rand) functions in a loop.
for($i = 0;$i < 256;$i++) {
for($j = 0;$j < 256;$j++) {
imagesetpixel($my_img, $i, $j, mt_rand(0, pow(2, 24)));
}
}
Edited by Alexander, 02 June 2011 - 09:15 AM.
Random samples