【问题标题】:PHP PNG 24 bit clean TransparencyPHP PNG 24 位清洁透明度
【发布时间】:2012-06-21 21:22:23
【问题描述】:

我一直在尝试以干净的 24 位 alpha 透明度上传 PNG。经过大量研究后,我设法让它工作了,但是透明度似乎是低质量的 8 位,正如您在此屏幕截图中看到的那样:

http://cozomo.com/apple.png

任何有助于实现干净的 PNG 上传和 24 位平滑透明度调整大小的帮助将不胜感激。我当前的代码如下。

if($extension=="png")
    {
        $uploadedfile = $_FILES['photo']['tmp_name'];
        $src = imagecreatefrompng($uploadedfile);
    }

        $dest_x = 1400; 
        $dest_y = 1200;

        if ($width > $dest_x or $height > $dest_y) { 

                if ($width >= $height) { 
                    $fullSize_x = $dest_x; 
                    $fullSize_y = $height*($fullSize_x/$width); 
                } else { 
                    $fullSize_x = $width*($fullSize_y/$height); 
                    $fullSize_y = $dest_y; 
                } 
        }

        $fullSize=imagecreatetruecolor($fullSize_x,$fullSize_y);

    //TEST
    $black = imagecolorallocate($fullSize, 0, 0, 0);
    imagecolortransparent($fullSize, $black);
    //TEST END

    // OUTPUT NEW IMAGES
    imagecopyresampled($fullSize,$src,0,0,0,0,$fullSize_x,$fullSize_y,$width,$height);

    imagepng($fullSize, "/user/photos/".$filename);

    imagedestroy($fullSize);


  [1]: http://i.stack.imgur.com/w8VBI.png

【问题讨论】:

  • 无论如何,32 位 PNG 的 alpha 通道只有 8 位分辨率。 RGB 为 24 位,alpha 为 8 位。
  • (顺便说一句,如果您希望图像显示在此处,请点击“17 秒前编辑”链接(或现在所说的任何内容)并点击我的“回滚”编辑。)
  • 这不能轻松工作,因为您告诉 GD '隐藏' 0,0,0 颜色但是 0,2,0 或 2,1,0 颜色是'黑色'但是不是 0,0,0?请提供原图进行测试。
  • @vlzvl 我不是 100% 确定你在这里说什么 - 但这是我使用的原始 PNG 图片inkscapegallery.net/files/images/apple.png

标签: php png transparency


【解决方案1】:

要保存完整的 Alpha 通道,您必须使用 imagesavealpha,在保存 png 之前输入它

imagealphablending($fullSize, false);
imagesavealpha($fullSize, true);

【讨论】:

  • 谢谢穆萨。我实际上早些时候尝试过,但我想我把它放在错误的地方。现在像魅力一样工作:)
【解决方案2】:

这是修改后的代码,感谢 Musa 为遇到相同问题的任何人提供帮助

function processPNG($pngImage) {
        $black = imagecolorallocate($pngImage, 0, 0, 0);
        imagecolortransparent($pngImage, $black);
        imagealphablending($pngImage, false);
        imagesavealpha($pngImage, true);
    }    


   if($extension=="png")
{
    $uploadedfile = $_FILES['photo']['tmp_name'];
    $src = imagecreatefrompng($uploadedfile);
}

    $dest_x = 1400; 
    $dest_y = 1200;

    if ($width > $dest_x or $height > $dest_y) { 

            if ($width >= $height) { 
                $fullSize_x = $dest_x; 
                $fullSize_y = $height*($fullSize_x/$width); 
            } else { 
                $fullSize_x = $width*($fullSize_y/$height); 
                $fullSize_y = $dest_y; 
            } 
    }

    $fullSize=imagecreatetruecolor($fullSize_x,$fullSize_y);
    if ($extension == "png") processPNG($fullSize);


// OUTPUT NEW IMAGES
imagecopyresampled($fullSize,$src,0,0,0,0,$fullSize_x,$fullSize_y,$width,$height);

imagepng($fullSize, "/user/photos/".$filename);

imagedestroy($fullSize);

【讨论】:

    猜你喜欢
    • 2013-01-08
    • 2011-07-31
    • 2011-06-04
    • 2010-09-23
    • 2013-09-20
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多