将透明png加载到gd php中但不要使其背景透明

问题描述 投票:1回答:1

我正在尝试从GD中的动态文本数据创建图像,并在顶角放置一个徽标。图像的背景颜色将根据传入的数据而变化,因此我不能将徽标另存为没有Alpha通道的图像。

我使用imagefill()创建图像,用动态背景颜色填充它,然后使用imagettftext()添加文本,然后加载我的徽标。我在将徽标放入图像时遇到问题而没有保留其背景颜色'透明'。所以我希望它背后的动态背景颜色是用imagefill()设置的。但是,它保持了加载的透明度背景,因此将png的这一部分写为透明。我在加载后尝试在徽标上调用imagefill()(使用设置目标图像背景的相同rgb),但这没有做任何事情。

以下是我的代码:

$background = $_GET['background'];
$data1 = $_GET['data1'];
$data2 = $_GET['data2'];
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];

$png_image = imagecreate(400, 200);
$gd_text_color = imagecolorallocate($png_image, 255, 255, 255);
$gd_background_color = imagecolorallocate($png_image, $r, $g, $b);
imagefill($png_image, 0, 0, $gd_background);

$text1 = "test test $data1";
$text2 = "test test again $data2";
$font = 'Lato-regular.ttf';
imagettftext($png_image, 18, 0, 20, 20, $gd_text_color, $font, $text1);
imagettftext($png_image, 18, 0, 20, 50, $gd_text_color, $font, $text2);

//trying to get this logo and place it in the corner.
$logo = imagecreatefrompng("images/logo.png");
imagecopy($png_image, $logo, 10, 10, 0, 0, 100, 30);

header('Content-type: image/png');
imagepng($png_image, $filename);
imagedestroy($png_image);

这是该代码的输出:http://i.imgur.com/n25h9Js.png这里是加载到接受alpha通道的程序时图像的样子:http://i.imgur.com/3OIRupN.png

有谁知道我是如何实现我正在尝试的?

谢谢你的时间。

编辑

尝试解释我想要的是另一张图片。顶部图像是我目前得到的,底部图像是我想要的。我只是想加载一个透明的PNG,可以坐在不同颜色的背景上。但是我要么像它在这里显示的那样(透明背景)还是黑色背景(因为我猜不会看到alpha通道?)。希望这可以帮助。图片:Error on top, mocked up version of how I want it below.

编辑2

根据下面的评论,我将它从imagecreate()更改为imagecreatetruecolor(),现在它工作正常!我想要解释为什么如果有人有时间这就解决了它,但是现在,谢谢所有评论或甚至花时间看这个问题的人。

php image png transparency gd
1个回答
0
投票

我怀疑你的imagecreate()可能会引起你的问题,因为它会创建一个没有真彩色图像的灵活性或广泛表现的palettised图像。

我建议你用imagecreatetruecolor()替换它。

© www.soinside.com 2019 - 2024. All rights reserved.