Php PNG文件不保持透明度?

Php PNG文件不保持透明度?,php,php-gd,Php,Php Gd,我将始终使用这些变量: $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($roo

我将始终使用这些变量:

$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
但是,我需要使用图像资源,以便在需要时可以操纵根文件

所以我说:

if ( file_exists( $NEWFILE ) ) unlink ($NEWFILE);
$image = imagecreatefrompng( $ROOTFILE );
imagepng( $image, $NEWFILE );
imagedestroy( $image );
现在当我使用这个:

<img src="<?=$NEWFILE?>" />
还是没有用


帮助?

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

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

PNG的透明度从来都不是完美的。如果背景是黑色,请尝试以下操作:

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

PNG中的透明度从来都不是完美的,因为PHP需要启用alpha混合并保存alpha。我在10秒的谷歌搜索后发现:
您需要启用alpha混合并保存alpha。我在10秒的谷歌搜索后发现: 这有帮助吗

$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");
这有帮助吗

$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");

这里的问题不在GD或PHP中

问题出在Photoshop上


如果在RGB模式而不是索引模式下保存PNG文件。

这里的问题不在GD或PHP中

问题出在Photoshop上


如果您在RGB模式而不是索引模式下保存PNG文件。

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

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

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

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

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

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

您说png的透明度在php中不好,还有其他方法吗?问题是,它需要所有的黑色,并使其透明。我只想保持原来的透明度。有什么想法吗?你看过imagemagick函数了吗?但如果普雷费尔特的答案让你分门别类,那就用它给他打勾!你说png的透明度在php中不好,还有其他方法吗?问题是,它需要所有的黑色,并使其透明。我只想保持原来的透明度。有什么想法吗?你看过imagemagick函数了吗?但如果普雷费尔特的答案让你分门别类,那就用它给他打勾!我刚刚搜索了imagecreatefrompng transparency。我阅读了imagecreatefrompng的函数文档,没有发现任何有用的东西,我的badi也发现用transparency保存8/16/24 png图像有困难,并且多次使用了一些脚本,生成了一些大的php块,试图覆盖每种情况下的true color或256等。。不是那么简单的东西。我只是搜索了imagecreatefrompng transparency。我阅读了imagecreatefrompng的函数文档,没有发现任何有用的东西,我的badi也发现用transparency保存8/16/24 png图像有困难,并且多次使用了一些脚本,生成了一些大php块,试图覆盖每种情况下的真彩色或256色等。。不是那么简单的东西。这实际上也为我在gimp中修复了它,而没有代码可以在gimp中修复它,而没有代码可以