简体   繁体   English

透明gif /黑色背景?

[英]Transparent gif/black background?

I have the following, it works except the gif is suppose to be transparent and it is creating a black background, if I change all the (imagesavealpha, etc) stuff to the $container then it created a transparent background for the text I add to this image. 我有以下内容,除了gif假定是透明的而且它正在创建黑色背景之外,它可以工作,如果我将所有(imagesavealpha等)内容更改为$ container,那么它将为我添加到的文本创建透明背景这个图片。 How would I go about getting rid of the black background? 我将如何摆脱黑色背景? Basically this is a signature type image. 基本上,这是一个签名类型的图像。 I write stuff too, which I don't think you need to see. 我也写东西,我认为你不需要看。

  $im = imagecreatefromgif("bg.gif");

  $container = imagecreatetruecolor(400, 160);
  imagesavealpha($im, true);
  imagealphablending($im, false);

  $trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
  $w = imagecolorallocate($container, 255, 255, 255);

  imagefill($im, 0, 0, $trans_colour);

  imagecopymerge($container, $im, 0, 0, 0, 0, 460, 180, 100);

The solution below is less complicated than the one you chose. 下面的解决方案比您选择的解决方案要简单。 It also addresses the question directly. 它还直接解决了这个问题。


$transparencyIndex = imagecolortransparent($imageSrc);
$transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);

if ($transparencyIndex >= 0) { $transparencyColor = imagecolorsforindex($imageSrc, $transparencyIndex);
}

$transparencyIndex = imagecolorallocate( $imageNew, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue'] ); imagefill($imageNew, 0, 0, $transparencyIndex); imagecolortransparent($imageNew, $transparencyIndex);

To resize and store the GIF image, imagecopyresampled() and imagegif() could be respectively used. 要调整大小和存储GIF图像,可以分别使用imagecopyresampled()和imagegif()。

I use these two functions to allow you to create a new transparent truecolour image... 我使用这两个功能来创建新的透明真彩色图像。

function image_createtruecolortransparent($x,$y) 
{
    $i = imagecreatetruecolor($x,$y);
    $b = imagecreatefromstring(base64_decode(image_blankpng()));
    imagealphablending($i,false);
    imagesavealpha($i,true);
    imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
    return $i;
}

function image_blankpng()
{
    $c = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
    $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";
    $c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";
    $c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";
    $c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";
    $c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";

    return $c;
} 

Which should do what you need. 哪个应该做您所需要的。 Just replace your function call to imagecreatetruecolor(400, 160); 只需将函数调用替换为imagecreatetruecolor(400,160); with image_createtruecolortransparent(400, 160); 与image_createtruecolortransparent(400,160); and include both functions. 并同时包含这两个功能。

This page is very useful in the PHP online manual: http://us2.php.net/manual/en/function.imagecolortransparent.php#89927 . 该页面在PHP在线手册中非常有用: http : //us2.php.net/manual/en/function.imagecolortransparent.php#89927 I linked to a specific comment, but it's always a good idea to at least have a look at most of them. 我链接到一个特定的评论,但是至少看看其中的大多数始终是一个好主意。

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

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