【问题标题】:PNG file is NOT keeping transparency?PNG文件不保持透明度?
【发布时间】:2012-05-29 22:09:15
【问题描述】:

我将在整个过程中使用这些变量:

$ROOTDIR = $_SERVER["DOCUMENT_ROOT"];
$ROOTFILE = "http://www.scottandjessiecooper.com/webtutorials/images/smiley.png";
$NEWFILE = "$ROOTDIR/images/tmp/new_smiley.png";

当我使用这个功能时,透明度没有问题

function save_image($root, $saveto){
    copy($root, $saveto);
}
save_image( $ROOTFILE, $NEWFILE ); // root can be file or url

但是我需要使用 IMAGE_RESOURCE,所以我可以根据需要操作 ROOTFILE

所以我做了这个:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
imagepng( $image, $NEWFILE );
imagedestroy( $image );

现在当我使用这个时:

<img src="<?=$NEWFILE?>" />

我失去了透明度。背景变黑了!

所以我尝试输出图像以确保不是导致问题的保存

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
header('Content-Type: image/png');
imagepng( $image );
imagedestroy( $image );

还是没用……

帮助?

【问题讨论】:

    标签: php php-gd


    【解决方案1】:

    您需要启用 Alpha 混合并保存 Alpha。我在 10 秒的谷歌搜索后发现了这个: http://www.php.net/manual/en/function.imagecreatefrompng.php#43024

    【讨论】:

    • 我刚刚搜索了“imagecreatefrompng 透明度”。
    • 我阅读了imagecreatefrompng 的函数文档,发现没有任何用处,我的错
    • 我也发现保存 8/16/24 png 透明图像有困难,并且多次使用一些脚本,产生一些大的 php 块试图覆盖每种情况(真彩色或 256 等)。不是那么容易的东西。
    【解决方案2】:

    我遇到了这个问题,发现 prehfeldt 的 answer 有正确的想法,但实际上并没有帮助我解决这个问题。启用保存 Alpha 通道信息的实用方法是在将图像资源输出到文件之前调用 imagesavealpha

    imagesavealpha($image, true);
    imagepng( $image, $NEWFILE );
    

    如果你不这样做,默认情况下GD会在你保存或输出图像时丢弃透明度信息。 copy 没有给您造成这个问题的原因是它在文件级别进行了简单的逐字节复制,根本没有经过任何图像处理。

    【讨论】:

      【解决方案3】:

      这里的问题不在GDPHP

      问题出在 Photoshop 中。

      如果您在RGB mode 而不是Indexed mode 上保存PNG 文件。

      【讨论】:

      • 这实际上也为我在 gimp 中修复了它,而没有代码可以
      【解决方案4】:

      这有帮助吗?

      $info = getimagesize("smiley.png");
      $image = imagecreatefrompng("smiley.png");
      $image_new = imagecreatetruecolor($info[0],$info[1]);       
      if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
        $trnprt_indx = imagecolortransparent($image);   
        if ($trnprt_indx >= 0 ) {   
           $trnprt_color    = imagecolorsforindex($image, $trnprt_indx);   
           $trnprt_indx    = imagecolorallocate($image_new, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);   
           imagefill($image_new, 0, 0, $trnprt_indx);   
           imagecolortransparent($image_new, $trnprt_indx);
        }
        elseif ($info[2] == IMAGETYPE_PNG) {
           imagealphablending($image_new, false);   
           $color = imagecolorallocatealpha($image_new, 0, 0, 0, 127);   
           imagefill($image_new, 0, 0, $color);   
           imagesavealpha($image_new, true);
         }
      }
      imagecopy($image_new,$image,0,0,0,0,$info[0],$info[1]);
      imagepng($image_new,"smiley2.png");
      

      【讨论】:

        【解决方案5】:

        如果背景是黑色的,请尝试以下操作:

        $black = imagecolorallocate($im, 0, 0, 0); 
        // Make the background transparent 
        imagecolortransparent($im, $black);
        

        (通过 PHP,PNG 的透明度从来都不是完美的)

        【讨论】:

        • 你说 png 的透明度在 php 中不好,还有其他方法吗?
        • 这个问题是,它把所有的黑色都变成了透明的。我只想保持原始透明度。有什么想法吗?
        • 你检查过 imagemagick 函数吗?但是,如果 prehfeldt 的回答让你得到了整理,请使用它(并给他打勾!)
        猜你喜欢
        • 2013-04-11
        • 2018-07-15
        • 2012-03-29
        • 1970-01-01
        • 2014-05-21
        • 1970-01-01
        • 2017-10-18
        • 1970-01-01
        • 2013-08-26
        相关资源
        最近更新 更多