PHP GD Lib PNG-24到PNG-8转换:生成的图像越大,保留的颜色越少

The following code works with intial 8bit-png's but when i use a 24 bit alpha transp png, it seems to work worse when the resulting image gets bigger. The bigger the resulting image the less colors remain. Both pictures where exported from PS via Web-Export.

How can I solve the problem? Is something wrong with the code or may it be a bug? (I am on Windows 10, PHP 5.6.6)

$imgPath = 'image-24bit-alpha-transp-from-ps-960x533.png';

$imgDirname   = pathinfo($imgPath, PATHINFO_DIRNAME);
$imgFilename  = pathinfo($imgPath, PATHINFO_FILENAME);
$imgExtension = pathinfo($imgPath, PATHINFO_EXTENSI

ON);

$newWidths = [80, 160, 320, 640, 960];

$img = imagecreatefrompng($imgPath);

list($width, $height) = getimagesize($imgPath);

if($width * $height > PHP_INT_MAX)
    throw new Exception('The picture exceeds the maximum number of pixels ('.INT_MAX.').');

$ratio = $height / $width;

$alphaTransparency = imagecolortransparent($img) >=0 || (ord(file_get_contents($imgPath, false, null, 25, 1)) & 4);
$numberOfColors    = imagecolorstotal($img);
$trueColorImage    = $numberOfColors === 0;

foreach($newWidths as $newWidth)
{
    $newImgPath = $imgDirname.DIRECTORY_SEPARATOR.$imgFilename.'_w'.$newWidth.'.'.$imgExtension;
    $newHeight  = ceil($newWidth * $ratio);

    $newImg = $trueColorImage ? imagecreatetruecolor($newWidth, $newHeight) : imagecreate($newWidth, $newHeight);

    imagealphablending($newImg, false);
    imagesavealpha($newImg,true);

    $color = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefill($newImg, 0, 0, $color);

    imagetruecolortopalette($newImg, false, $trueColorImage ? 255 : $numberOfColors);

    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

    imagepng($newImg, $newImgPath, 6);
    imagedestroy($newImg);
}

imagedestroy($img);