如何在带有GD的png 24 alpha透明图像中将一种颜色替换为另一种颜色 [英] How can I replace one color with another in a png 24 alpha transparent image with GD
本文介绍了如何在带有GD的png 24 alpha透明图像中将一种颜色替换为另一种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试过了:
$index = imagecolorresolve ( $im, 0,0,0 ); // get black
imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR
这似乎适用于png 8而不是24,如果我这样做的话因为抗锯齿,所以很奇怪。
This seems to work with png 8 but not 24, and if I do it with 8 it turns out all weird because of the anti-aliasing.
这是我正在使用的完整测试代码。 (这只是测试代码,所以要温和)。
Here is the full test code I'm using. (this is just test code, so be gentle).
function LoadPNG($imgname, $color = false)
{
$im = @imagecreatefrompng($imgname);
imagealphablending($im, false);
if($color) {
$index = imagecolorresolve ( $im, 0,0,0 ); // get black
imagecolorset($im, $index, 255, 0, 255); // SET NEW COLOR
}
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
return $im;
}
header('Content-Type: image/png');
$img = LoadPNG("head.png", "red");
imagepng($img);
imagedestroy($img);
推荐答案
您可以尝试以下方法:
- 循环所有积分
- 获取该点的颜色
- 如果它匹配你的颜色A,将该像素设置为所需的颜色B
代码:
for ($x=imagesx($im); $x--; ) {
for ($y=imagesy($im); $y--; ) {
$c = imagecolorat($im, $x, $y);
if ($c[0] == 0 && $c[1] == 0 && $c[2] == 0) {
// here we use the new color, but the original alpha channel
$colorB = imagecolorallocatealpha($im, 255, 0, 255, $c[3]);
imagesetpixel($im, $x, $y, $colorB);
}
}
}
希望这有帮助!
这篇关于如何在带有GD的png 24 alpha透明图像中将一种颜色替换为另一种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文