开发者

Black background when turning a PNG into a GIF

I have a PNG image I am trying to open and then output as a GIF image. However, the transparency is lost when I do this and the background turns black. If I output the image as a PNG, it works, but I specifically need to open the image as a PNG and the output it as a GIF.

This is what I have so far:

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

 $new_img = imagecreatefrompng($image);
 imagealphablending($new_img, false); 
 images开发者_运维技巧avealpha($new_img, true); 
 imagegif($new_img);
 ?>

However, imagepng($new_img) saves the background transparency but does not output as a GIF.


Try with this code:

<?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);
?>


So I managed to get this to work. Please let me know if anyone has a better solution:

 <?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);
  ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜