2012-08-12 83 views
-1

我想創建一個簡單的PHP,這將需要一些INT輸入,並利用它們來繪製一個矩形,但下面的功能不能正常工作的功能...PHP的繪圖功能

<?php 

$img = imagecreatetruecolor(500, 500); 

$white = imagecolorallocate($img, 255, 255, 255); 
$red = imagecolorallocate($img, 255, 0, 0); 
$green = imagecolorallocate($img, 0, 255, 0); 

//set canvas background to white 
imagefill($img, 0, 0, $white); 


//THIS FUNCTION IS NOT WORKING 
function draw($x1Pos, $y1Pos, $x2Pos, $y2Pos, $colour) { 

    imagerectangle($img, $x1Pos, $y1Pos, $x2Pos, $y2Pos, $colour); 
} 

draw(20, 40, 60, 80, $red); 
draw(30, 40, 80, 100, $green); 



imagerectangle($img, 150, 100, 300, 250, $green); 
imagerectangle($img, 100, 100, 200, 200, $blue); 

header("Content-type: image/png"); 
imagepng($img); 
imagedestroy($img); 

?> 
+0

它無法顯示,因爲它包含錯誤? – themis 2012-08-12 04:55:29

回答

0

你的代碼由於兩個原因失敗;首先,$img從不傳遞給函數。您可能需要在函數中將其聲明爲全局函數,或者通過參數傳遞它。其他,$blue不存在。替換或實際寫入它,並且你的代碼工作正常。

+0

現在確定它的工作,對於藍色的事情抱歉,它確實存在,但我沒有將它複製到上面的示例中。關於$ img,它不在函數中,爲什麼我仍然需要解析它?該功能不應該訪問它嗎? – user1334130 2012-08-12 05:03:00

+0

http://www.php.net/manual/en/language.variables.scope.php – 2012-08-12 05:14:54

+0

@ user1334130不,它不應該。正如Leon所說的那樣,這並不是變量在php中的作用域。 – Daedalus 2012-08-12 05:37:41