2012-08-12 80 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