Joe Lencioni
Joe Lencioni

Reputation: 10511

ImageTrueColorToPalette losing colors

I have a PHP script that converts true color images to palette images if the number of different colors in the image is less than or equal to 256.

In my test case, I have an image that contains 79 colors. After running ImageTrueColorToPalette on it without dither and the $ncolors parameter set to 79, I have an image that only has 15 colors in it. I have even tried running ImageColorMatch on it after converting it to palette, and I still end up with only 15 colors.

Using GD in PHP, how can I convert my true color image to a palette image without losing all of those colors?

Upvotes: 1

Views: 981

Answers (1)

Joe Lencioni
Joe Lencioni

Reputation: 10511

It appears I can convert the true color image to a palette image by creating a palette image using ImageCreate and then copying the true color image to that one. This produces much better results than ImageTrueColorToPalette.

Why ImageTrueColorToPalette gives me a crappy looking image, I'm not really sure, but this seems like an adequate workaround.

$palette = ImageCreate($width, $height);
ImageCopy($palette, $truecolor, 0, 0, 0, 0, $width, $height);

Upvotes: 1

Related Questions