2012-05-24 277 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);