繁体   English   中英

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

[英]Black background when turning a PNG into a GIF

我有一个我试图打开的 PNG 图像,然后将 output 作为 GIF 图像。 但是,当我这样做并且背景变黑时,透明度会丢失。 如果我将 output 图像作为 PNG 格式,它可以工作,但我特别需要将图像作为 PNG 和 output 作为 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) 保存背景透明度,但不将 output 保存为 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);
  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM