2012-05-24 223 views
1
$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位,所以它没有任何错误的创建图像代码。图片GD - 白色背景

感谢

回答

2

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

并且您无法将imagecopy从[color]采样到[image],您应该从[image]到[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); 
0

http://php.net/manual/en/function.imagecopyresampled.php指出你的前两个参数必须$dst_image$src_image

$new_img不会在你的代码存在,需要与imagecreatetruecolor();

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

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);