1
我尝试使用gd库重新着色单色png图像。我在另一篇文章中发现了一个代码,它会重新着色gif。我修改了该代码以使用透明png。这是我的代码。我可以用这段代码创建一个新的png文件,但颜色不会改变。请有人帮我改变透明PNG的颜色。使用gd libarary在单一颜色透明PNG中更改颜色
<?php
// first we will create a transparent image. an image that has no color.
$width = 300; $height=340;
$image = imagecreatetruecolor($width,$height); //black image of the specified width x height.
imagealphablending($image, false); // set blend mode to false.
$col=imagecolorallocatealpha($image,255,255,255,127); // fill color
imagefilledrectangle($image,0,0,$width,$height,$col);
imagealphablending($image,true);
$shirt = imagecreatefrompng("shirt.png");
$color = imagecolorclosest ($shirt, 255,0,0);
imagecolorset($shirt,$color,92,92,92); // SET NEW COLOR
imagecopy($image, $shirt, 0, 0, 0, 0, $width, $height);
imagealphablending($image,true);
imagealphablending($image,false);
imagesavealpha($image,true);
if(imagepng($image, "hello.png", 1)){
echo "hello.png";
}
imagedestroy($image);
imagedestroy($shirt);
?>