【问题标题】:How to make an image itself Transparent/Alpha with GD?如何使用 GD 使图像本身透明/Alpha?
【发布时间】:2013-12-03 08:56:57
【问题描述】:

在 PHP GD 中,是否可以使图像本身透明?我不是在说背景等。是否可以拍摄PNG图像,并使图像透明?

例如:

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

imagepng($c);

然而,这并没有做任何事情。有什么我想念的吗?谢谢。

【问题讨论】:

    标签: php php-gd


    【解决方案1】:

    您忘记使用imagecolortransparent() 函数。更多: http://www.php.net/manual/en/function.imagecolortransparent.php

    取自 cmets 这里有一个增加透明度的例子

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

    【讨论】:

    • 你好。查看代码并阅读上述评论后,看起来它所做的只是将每个黑色像素更改为透明像素。我要做的是获取图像中的每个像素,并使其透明。你如何建议我这样做?
    • 如果您需要将任何单个像素转换为透明像素,只需创建一个黑色的新图像并使用 imagecolortransparent() fx。
    • 我很抱歉,但这不是我的问题。我的问题是是否可以使用 PHP GD 获取 PNG 图像,并获取每个像素并使其半透明(alpha 约为 65)。
    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多