有時此函式會產生難看/暗淡的顏色(尤其是在 ncolors < 256 時)。以下是一個替代方案,它使用暫存影像和 ImageColorMatch() 來更準確地匹配顏色。它可能會稍微慢一些,但檔案大小最終會相同
<?php
函數 ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
$width = imagesx( $image );
$height = imagesy( $image );
$colors_handle = ImageCreateTrueColor( $width, $height );
ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
ImageTrueColorToPalette( $image, $dither, $ncolors );
ImageColorMatch( $colors_handle, $image );
ImageDestroy( $colors_handle );
}
?>