2011-09-30 51 views
3

該功能是用戶上傳圖片時,程序會生成一個新的白色背景的squre圖片,並將用戶的圖片放在該圖片的中心。使用php生成圖片,背景總是顏色錯誤

但問題是,我將背景設置爲白色,它總是顯示黑色。

的代碼是

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

和控制顏色的代碼是 protected $background = "255,255,255";

我一直在試圖改變$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]);$white = imagecolorallocate($capture, 255, 255, 255);。但背景仍然以黑色顯示。

感謝您的任何答案

+0

「不工作」是什麼意思?背景顏色是什麼?另外,你如何渲染圖像? –

+0

@Zach Rattner它仍然以黑色而不是白色顯示。我使用imagejpeg來渲染圖像 – Juni

+1

'''header('Content-Type:image/jpeg'); $ im = @imagecreatetruecolor(120,20) or die('Can not Initialize new GD image stream'); $ rgb = explode(「,」,「255,255,255」); $ white = imagecolorallocate($ im,$ rgb [0],$ rgb [1],$ rgb [2]); imagefill($ im,0,0,$ white); imagejpeg($ im);'''適合我,你確定你以後不會覆蓋它嗎? – Deebster

回答

3

從手動imagecreatetruecolor() returns an image identifier representing a black image of the specified size.第一次調用imagecolorallocate設置基於調色板在背景,但真彩色圖像是沒有的。

我在真彩色圖像上設置背景顏色的方法是隻填充一個實心的矩形。

imagefilledrectangle($capture, 0, 0, $width, $height, $white); 
+0

矩形工作,但我也需要更改'$ rgb = explode(「,」,$ this-> background); '到'$ rgb = explode(「,」,「255,255,255」);' – Juni

0

imagefill($im, x, y, $color)

$red = imagecolorallocate($im, 255, 0, 0); 
imagefill($im, 0, 0, $red); 

這將填充整個圖像與紅色,和x,y參數是矩形填充的起點。