PDA

View Full Version : problems generating an image using imageline($image, $x1, $y1, $x2, y2, $black) help



ginnybrighton
05 Jun 2010, 12:55 PM
hi,

i am a total noob when it comes to generating images using php.

I followed a simple tutorial in a php book to generate an image with a single line using the


imageline($image, 10, 10, 450, 10, $black);

function. This worked fine. The next task was to generate a series of lines based on values stored in a mysql database. this is where i am having a problem. Instead of an image, i get a little box with an x in it.

I have pasted my code below as i am now at my wits end. 4 days looking at the same problem from a number of angles and still no luck. Im going crosseyed! Can someone please help me.

These are the things i have already tried and no success:

1) is the 2 dimensional array not being read in properly? no! the array is being read in fine... i do a count($array); call and all the x coordinates and all the y coordinates are there?

2) are they appearing as objects, not numbers? No! I multiply everything *1 to make sure.

3) does the php not like generating 2 lines in exactly the same place? no!


imageline($image, 10, 10, 450, 10, $black);

imageline($image, 10, 10, 450, 10, $black);
works just fine...

does anyone know where im going wrong??? thanks a million:

My function:

function domake($a1){
global $width;
global $height;
global $image;
global $black;
$array = $a1;



$count = count($array);
//echo "count $count";

$width = 1000;
$height = 1000;
$image = imagecreate($width, $height);
$black = imagecolorallocate($image, 0, 0, 0);


for ($i = 0; $i <= $width; ++$i) {
for ($j = 0; $j <= $height; ++$j) {
$col = imagecolorallocate($image, 255, 255, 255);
imagesetpixel($image, $i, $j, $col);
}
}

$x = -1;
$y = -1;

//// get contents of array
for($arr=0; $arr<$count; $arr++){
$key = $arr%2;
$x = $array[$arr][0]*1;
$y = $array[$arr][1]*1;

//if x and y are both greater than -1 make line
$x1 = $x;
$y1 = $y;
$x2 = $x+450;
$y2 = $y;
if(($x>=0)&&($y>=0)){
imageline($image, $x1, $y1, $x2, $y2, $black);
}
}


// imageline($image, 10, 10, 450, 10, $black);

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