[英]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.我有一个我试图打开的 PNG 图像,然后将 output 作为 GIF 图像。 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.
如果我将 output 图像作为 PNG 格式,它可以工作,但我特别需要将图像作为 PNG 和 output 作为 GIF 打开。
This is what I have so far:这是我到目前为止所拥有的:
<?php
header("Content-type: image/gif");
$new_img = imagecreatefrompng($image);
imagealphablending($new_img, false);
imagesavealpha($new_img, true);
imagegif($new_img);
?>
However, imagepng($new_img) saves the background transparency but does not output as a GIF.但是, imagepng($new_img) 保存背景透明度,但不将 output 保存为 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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.