2
$im = imagecreatefrompng('./test.png');
$white = imagecolorallocate($im, 255, 255, 255);
imagecolortransparent($im, $white);

此代码适用于从图像中删除纯白色,但我想要做的是将所有颜色转换为 alpha 百分比。例如,中等灰色将是 50% 透明黑色。因此,如果放置在顶部,中等灰色的图像将显示其后面的图像。

这可以使用 PHP 或扩展吗?

4

1 回答 1

0

我刚刚为你编写并测试了这个脚本。

$imfile = './test.png';
$origim = imagecreatefrompng($imfile);
$im_size = getimagesize($imfile);

$im = imagecreatetruecolor($im_size[0], $im_size[1]);
imagealphablending($im, false);
imagesavealpha($im, true);

for ($x = 0; $x < $im_size[0]; ++$x){
    for ($y = 0; $y < $im_size[1]; ++$y){
        $rgb = imagecolorat($origim,- $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $color = imagecolorallocatealpha($im, 0, 0, 0, intval(($r+$g+$b)/3/255*127)); 
        imagesetpixel($im, $x, $y, $color);
    }
}

在大图像上它可能有点慢,但它就像你想要的那样工作。=)

希望这个答案是可以接受的。祝你好运。

于 2011-07-19T23:04:59.210 回答