如何使用GD使图像本身透明/Alpha


How to make an image itself Transparent/Alpha with GD?

在PHP GD中,是否可以使图像自透明?我不是在谈论背景等等。有可能拍摄PNG图像并使图像透明吗?

例如:

$c = imagecreatefrompng($urlToImage);
imagefill($c, 0, 0, imagecolorallocatealpha($c, 255, 255, 255, 50)); // 50 alpha.
imagepng($c);

然而,这并没有起到任何作用。我有什么东西不见了吗?非常感谢。

您忘记使用imagecolortransparent()函数。更多信息,请访问:http://www.php.net/manual/en/function.imagecolortransparent.php

从评论中可以看出,这里有一个添加透明度的例子

if($transparency) {
        if($ext=="png") {
            imagealphablending($new_img, false);
            $colorTransparent = imagecolorallocatealpha($new_img, 0, 0, 0, 127);
            imagefill($new_img, 0, 0, $colorTransparent);
            imagesavealpha($new_img, true);
        } elseif($ext=="gif") {
            $trnprt_indx = imagecolortransparent($img);
            if ($trnprt_indx >= 0) {
                //its transparent
                $trnprt_color = imagecolorsforindex($img, $trnprt_indx);
                $trnprt_indx = imagecolorallocate($new_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                imagefill($new_img, 0, 0, $trnprt_indx);
                imagecolortransparent($new_img, $trnprt_indx);
            }
        }
    } else {
        Imagefill($new_img, 0, 0, imagecolorallocate($new_img, 255, 255, 255));
    }