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);
?>