Well, if you have GD2, you can avoid that by using a true color image.
However, if you use GD1, then you need to do this to avoid screwing up the pallette:
create a new image (this is the canvas you will end up with)
copy the old image onto the canvas
copy the new image over the canvas
replace the old image with the canvas
destroy the canvas and new image
Otherwise, you'd have to loop through every color and allocate a spot for it in the pallette, and that wouldn't work very well. (On PNG, for instance, 256-(colors from new image) colors are left).