I’m using imagesetpixel() to draw a gray pixel for the letter ‘x’ and a black pixel for the letter ‘o’. It is working fine if you only enter in ~250 or less X’s and O’s, but if you enter in more than that, for some strange reason, only the first ~250 X’s and O’s are drawn, and the rest are not. Does anyone know how I can fix this?
As a sidenote, in the future, I may want to have the ability to be able to substitute additional letters for their own colors I choose, so please keep that in mind (e.g. a = red, b = blue, c = green, d = orange, etc.).
...
$image = imagecreate($xdim, $ydim);
for(...){
if( $string{$i} == "o" ){
$color = 0;
} else if( $string{$i} == "x" ){
$color = 170;
}
imagesetpixel($image, $x, $y, imagecolorallocate($image, $color, $color, $color));
...
}
The function imagecolorallocate adds a colour to the image colour table every time you call it — this colour table has a limit of 255 colours.
What you want to do is create references to the colours before you loop through all your letters, effectively only creating one colour reference per letter.
So allocate references to your different colours just after creating your image, then call directly these references instead of allocating a new colour: