Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将PNG转换为GIF时的黑色背景_Php - Fatal编程技术网

Php 将PNG转换为GIF时的黑色背景

Php 将PNG转换为GIF时的黑色背景,php,Php,我有一个PNG图像,我正试图打开,然后作为GIF图像输出。但是,当我这样做时,透明度会丢失,背景会变黑。如果我将图像输出为PNG,它可以工作,但我特别需要将图像打开为PNG,并将其输出为GIF 这就是我到目前为止所做的: <?php header("Content-type: image/gif"); $new_img = imagecreatefrompng($image); imagealphablending($new_img, false); imagesavealp

我有一个PNG图像,我正试图打开,然后作为GIF图像输出。但是,当我这样做时,透明度会丢失,背景会变黑。如果我将图像输出为PNG,它可以工作,但我特别需要将图像打开为PNG,并将其输出为GIF

这就是我到目前为止所做的:

 <?php
 header("Content-type: image/gif");

 $new_img = imagecreatefrompng($image);
 imagealphablending($new_img, false); 
 imagesavealpha($new_img, true); 
 imagegif($new_img);
 ?>

但是,imagepng($new_img)会保存背景透明度,但不会输出为GIF。

请尝试以下代码:

<?php
header("Content-type: image/gif");

$new_img = imagecreatefrompng($image);
$trans_color = imagecolortransparent($new_img);
$trans_index = imagecolorallocate($new_img, $trans_color['red'], $trans_color['green'], $trans_color['blue']);
imagecolortransparent($new_img, $trans_index);
imagegif($new_img);
?>

所以我设法让它工作起来。请告诉我是否有更好的解决方案:

 <?php
  header("Content-type: image/gif");

  $new_img = imagecreatefrompng($image);
  $background = imagecreatefrompng("background.png");
  imagecopyresampled($background, $new_img, 0, 12, 0, 0, 100, 125, 100, 125);
  $c = imagecolorat($background, 0, 0);
  imagefilledrectangle($background, 0, 112, 100, 125, $c);
  imagecolortransparent($background, $c);
  imagegif($new_img);
  ?>


噢,如果我首先将图像创建为gif格式并执行此操作,效果会很好。所以这不是我的浏览器。正是从PNG到GIF的转换把事情搞砸了。@sombe-“大多数主流浏览器都不支持GIF的透明性”,我认为1800年以来的所有浏览器都支持GIF的透明性。这似乎也不起作用。我认为主要的问题是从PNG到GIF的转换,所以如果有办法将图像转换成GIF,然后在将整个内容输出到浏览器之前弄乱透明度的话。