如何通过PHP中的Imagick将PNG32转换为PNG8

dod*_*dge 4 php png imagick

我想通过php Object Imagick将PNG32转换为PNG8.但我使用setImageDepth和setImageFormat设置param为8bit,它没有生效.像这样的代码:

$im = new Imagick($image);
$im->cropImage($cutWidth,$cutHeight,$x,$y);
$im->thumbnailImage($maxWidth, $maxHeight); 
$im->setImageDepth(8);
$im->setImageFormat('PNG8');
$im->writeImage($filename);
Run Code Online (Sandbox Code Playgroud)

inputfile是PNG32,但上面的输出仍然是PNG8,有其他解决方案吗?

Yid*_*nja 12

这似乎是一个已知的问题,所以我做了一些研究.基本上,setImageDepth还不够.您需要对图像进行量化.这是一个适合我的测试脚本......

$im = new imagick('stupid.png'); //an image of mine
$im->setImageFormat('PNG8');
$colors = min(255, $im->getImageColors());
$im->quantizeImage($colors, Imagick::COLORSPACE_RGB, 0, false, false );
$im->setImageDepth(8 /* bits */);
$im->writeImage('stupid8.png');
Run Code Online (Sandbox Code Playgroud)

出来很好.