php gd 返回黑色背景,图像创建器函数返回页面中央的图像

问题描述 投票:0回答:2

我正在使用 PHP-GD。当我尝试使用 PHP 中的“imagecreatetruecolor”函数创建图像时,会生成图像。但这里的主要问题是由此生成的图像具有代码中指定的黑色背景,但其余部分整个页面也会变成灰色,并且无论 imagecreatetruecolor 函数中给出的属性如何,图像都会显示在窗口的中心。我已经在 PHP-GD 上工作了四个月,但从来没有发生过这种情况。但从上周开始,它的表现很尴尬。请帮我解决。

<?php

header("Content-type: image/png");

$img = imagecreatetruecolor(400, 400);

$white = imagecolorallocate($img, 255, 255, 255);
$red   = imagecolorallocate($img, 255,   0,   0);
$green = imagecolorallocate($img,   0, 255,   0);
$blue  = imagecolorallocate($img,   0,   0, 255);

imagefilledellipse($img,100,100,10,10,$green);
imagefilledellipse($img,200,200,10,10,$green);

imagepng($img);

imagedestroy($img);

?>

This is the output when i try to execute the above code.As you see the image i created using php gd functions returns a image at the center of the window rather than attributes added to the function imagecreatetruecolor

php html php-gd
2个回答
0
投票

这是Chrome的结果,而不是GD的结果。最新版本的 Chrome 使图像在窗口中居中,并为窗口的其余部分提供黑色背景。

如果您想在操作中看到这一点,请添加以下代码为您的图像提供白色背景:

imagefill($img, 0, 0, $white);

这将为您提供以下信息:

enter image description here


0
投票

我知道这可能是一个老问题,但是我也遇到了同样的问题,这是由于我的原始图像具有透明度。 调整大小后,透明像素呈现为黑色、白色或透明。 由于我正在制作 QR 码(这就是您正在做的事情),因此我在 QR 码创建步骤中删除了透明度,并且随后的大小调整保持了带有黑点的白色背景。 希望这对某人有帮助。

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