$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位,一个法师会抽出来,所以创建图像代码并没有错。
由于
答案 0 :(得分:2)
您使用分配的白色在imagefill()
上缺少$background
方法。
并且你无法从[color]到[image]进行图像采样,你应该从[图像]到[图像]进行采样。
$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);
答案 1 :(得分:0)
http://php.net/manual/en/function.imagecopyresampled.php声明前两个参数需要为$dst_image
和$src_image
。
$new_img
,需要使用imagecreatetruecolor();
默认情况下,我认为它应该是白色背景,因此您不需要imagecolorallocate()
答案 2 :(得分:0)
我希望这可行
$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);