PDA

View Full Version : Generating images with lines in PHP



ginnybrighton
06 Jun 2010, 03:08 AM
Hi all,

This is my first-ish time using this forum - I tried once yesterday but my post never materialised. I think it had something to do with forgetting the PHP tags.

Anyhoo, Im still having trouble with drawing lines generated in PHP based on numeric values passed from an array. Maybe someone will know what i am doing wrong. It is really driving me crazy :bounce:

I started with a very basic - here we go inserting php into my post... sure hope it works this time.


$image = imagecreate($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imageline($image, 0, 0, 200, 0, $black);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);


This works perfectly.

But when I try to introduce a bit of dynamicity to the script it c raps out!!! :splat:

I hope to end up with a family tree being drawn with vertical and horizontal lines. I am only on the horizontal lines at the mo... so i know i have a long way to go...

At the moment when i run the script, all I get is a little box with an x on the page so I figure I have created an image but there is nothing in it? Is that right?

I know its not the array, per se, that is causing trouble because if i hard code in a static array at the beginning of the script, and call that one using



$array[0][0] = 10;
$array[0][1] = 10;
$array[1][0] = 20;
$array[1][1] = 20;


I then add 200 to the x1 value to get the x2 value. You will see when you read my code what i am talking about. Anyway, this works fine too...

I thought it might be that the values being read from the database are appearing as objects and not numeric values - i read somewhere that this can be a problem - so i multiplied each of the values by *1 before inserting them into the imageline( function.

this wasnt the problem eighter - gurrrrrr!

Then, I thought that it was because I had the beginning of my drawline, the middle of my drawline, and the end of my drawline in seperate functions so i put them all in a function called domake and that turned out not to be the problem either.

I have come up with a lot more zany solutions in the past 4 days to no avail so now i come here to seek help ... so please please please HELP - and then go play the lottery because one good deed deserves another and you will surely win for helping me

My code is as follows



$myImage = "";
$myWhite = "";
$myBlack = "";
$array = "";
$width = "";
$height = "";
$image = "";
$black = "";


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)){
echo "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);
}


Thanks so much for taking the time to read my post and give me some helpful hints...

:confused: