用php生成图片,背景总是颜色不对

Jun*_*uni 7 php gd image

功能是当用户上传一张图片时,程序会生成一个新的白色背景的方形图片,并将用户的图片放在这张图片的中心。

但问题是,我将背景设置为白色,它总是显示黑色。

代码是

$capture = imagecreatetruecolor($width, $height);
$rgb = explode(",", $this->background); 
$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]); 
imagefill($capture, 0, 0, $white); 
Run Code Online (Sandbox Code Playgroud)

和控制颜色的代码是 protected $background = "255,255,255";

我一直在尝试更改$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]);$white = imagecolorallocate($capture, 255, 255, 255);. 但是背景仍然显示为黑色。

感谢您的任何回答

dre*_*010 6

来自手册imagecreatetruecolor() returns an image identifier representing a black image of the specified size. 第一次调用 imagecolorallocate 为基于调色板的图像设置背景,但不是真彩色图像。

我在真彩色图像上设置背景颜色的方法是用实心矩形填充它。

imagefilledrectangle($capture, 0, 0, $width, $height, $white);
Run Code Online (Sandbox Code Playgroud)