图像GD - 白色背景


Image GD - White Background

$background = imagecreatetruecolor(709,709);
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagecopyresampled($whiteBackground, $new_img,(709-$imageWidth)/2,(709-$imageHeight)/2, 0, 0, $imageWidth, $imageHeight, $width, $height);
ImageJpeg ($background,"$path/$newName.$file_ext", 85);

我试图用GD创建一个白色背景的图像。但是没有运气,知道我做错了什么吗?我知道如果我取出 whiteBackground 位,法师会抽出,所以创建图像代码没有错。

谢谢

您在$background上缺少imagefill()分配的白色的方法。

而且您不能从[颜色]到[图像]进行图像复制重新采样,您应该从[图像]到[图像]进行。

$background = imagecreatetruecolor(709,709);
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground);
imagecopyresampled($background, $new_img,(709-$imageWidth)/2,(709-$imageHeight)/2, 0, 0, $imageWidth, $imageHeight, $width, $height);
ImageJpeg ($background,"$path/$newName.$file_ext", 85);

http://php.net/manual/en/function.imagecopyresampled.php 指出前两个参数需要$dst_image$src_image

$new_img 代码中不存在,需要使用 imagecreatetruecolor(); 创建

默认情况下,我相信它应该是白色背景,所以你不需要imagecolorallocate()

我希望这有效

$background = imagecreatetruecolor(709,709);
$whiteBackground = imagecolorallocate($background, 255, 255, 255);
imagefill($background,0,0,$whiteBackground);
$use_new_img = imagecreatefromjpeg($new_img);
imagecopyresampled($whiteBackground,$use_new_img,(709-$imageWidth)/2,  (709-$imageHeight)/2, 0, 0,$imageWidth,$imageHeight,$width,$height);
ImageJpeg ($background,"$path/$newName.$file_ext", 85);