有没有办法将图像限制为15种颜色(加上透明度)的自定义调色板?我想使用PHP函数将图像的调色板调整为一组特定的颜色。
现在我正在使用imagecreatefrompng()加载图像,但它们包含默认的RGB调色板。我还有一个4x4px图像,每种颜色都有。
我尝试过使用imagepalettecopy(),但它似乎没有任何效果。
$palette = imagecreatefrompng(public_path() . "/assets/palette2.png");
$original = imagecreatefrompng(public_path() . '/uploads/test1.png');
$resampled = imagecreatetruecolor(5, 5);
imagepalettecopy($resampled, $palette);
imagecopyresampled($resampled, $original, 0, 0, 0, 0, 5, 5, imagesx($original), imagesy($original));
imagepalettecopy($resampled, $palette);
谢谢!
答案 0 :(得分:0)
我发现以下内容可以很好地与纯PHP一起使用。不确定这是如何叠加性能的,但它提供了所需的结果:
$palette = imagecreatefrompng(public_path() . "/assets/palette.png");
$original1 = imagecreatefrompng(public_path() . '/uploads/test1.png');
$resampled1 = imagecreatetruecolor(5, 5);
$adjusted1 = imagecreatetruecolor(5, 5);
imagetruecolortopalette($palette, false, 16);
imagecopyresampled($resampled1, $original1, 0, 0, 0, 0, 5, 5, imagesx($original1), imagesy($original1));
for ($row = 0; $row < 5; $row++) {
for ($column = 0; $column < 5; $column++) {
$rgb1 = imagecolorat($resampled1, $column, $row);
$color1 = imagecolorsforindex($resampled1, $rgb1);
$closestRgb1 = imagecolorclosest($palette, $color1['red'], $color1['green'], $color1['blue']);
$closestColor1 = imagecolorsforindex($palette, $closestRgb1);
imagesetpixel($adjusted1, $column, $row, imagecolorallocate($adjusted1, $closestColor1['red'], $closestColor1['green'], $closestColor1['blue']));
}
}