To correct m4551 at abasoft dot it example:
ImageTrueColorToPalette($im,1,$t);
might give less colors than $t, so the for loop should call "$i<ImageColorsTotal($im)" instead of "$i<$t" just to be sure, or you'll get the warning: Color index [0-9] out of range