# having trouble using gd imageline in foreach loop

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
I am trying to take the first 15 numbers of the fibonacci sequence and use them
to draw a squarish spiral.
I have an array with the first 15 numbers \$fibsequence.
I am looping through this array so that a line correponding to the fibonacci
value will be drawn. I am alternating "directions": north, south, east, west. I
am starting from the center of the image.

For some reason I am only getting one straight line going east instead of the
four alternating directions.

Here is my code:

<?php

\$fibsequence=[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610];
\$fibseries = \$fibsequence;
\$imagewidth = array_pop(\$fibseries); // will be 610
\$imageheight = array_pop(\$fibseries); // will be 377
\$im = imagecreatetruecolor(\$imagewidth,\$imageheight)
or die('Cannot Initialize new GD image stream');
\$background_color = imagecolorallocate(\$im, 224, 234, 234);
\$text_color = imagecolorallocate(\$im, 233, 14, 91);
imagefill(\$im, 0, 0, \$background_color);

\$direction = "north";
\$x1 = bcdiv(\$imagewidth,2); // image center on x axis
\$y1 = bcdiv(\$imageheight,2); // image center on y axis
foreach(\$fibsequence as \$fibvalue){
if(\$direction=="north"){ \$x2 = \$x1; \$y2 = bcsub(\$y1,\$fibvalue);
\$direction="west"; }
if(\$direction=="west"){ \$x2 = bcsub(\$x1,\$fibvalue); \$y2 = \$y1;
\$direction="south"; }
if(\$direction=="south"){ \$x2 = \$x1; \$y2 = bcadd(\$y1,\$fibvalue);
\$direction="east"; }
if(\$direction=="east"){ \$x2 = bcadd(\$x1,\$fibvalue); \$y2 = \$y1;
\$direction="north"; }
imageline(\$im,\$x1,\$y1,\$x2,\$y2,\$text_color);
imagestring(\$im,1,\$x2,\$y2,\$fibvalue,\$text_color);
\$x1 = \$x2; // actual x ending point becomes next x starting point
\$y1 = \$y2; // actual y ending point becomes next y starting point
}
ob_start();
imagepng(\$im);
\$fibspiral = ob_get_contents();
ob_end_clean();
echo "<img src='data:image/png;base64,".(base64_encode(\$fibspiral))."' />";
?>

How come I am getting a straight line? If I try doing 15 consecutive

imageline(\$im,305,189,305,188,\$text_color); // y2=y1-1
imageline(\$im,305,188,304,188,\$text_color); // x2=x1-1
imageline(\$im,304,188,304,190,\$text_color); // y2=y1+2
imageline(\$im,304,190,307,190,\$text_color); // x2=x1+3
imageline(\$im,307,190,307,185,\$text_color); // y2=y1-5
imageline(\$im,307,185,299,185,\$text_color); // x2=x1-8
imageline(\$im,299,185,299,198,\$text_color); // y2=y1+13
imageline(\$im,299,198,320,198,\$text_color); // x2=x1+21
imageline(\$im,320,198,320,164,\$text_color); // y2=y1-34
imageline(\$im,320,164,265,164,\$text_color); // x2=x1-55
imageline(\$im,265,164,265,253,\$text_color); // y2=y1+89
imageline(\$im,265,253,409,253,\$text_color); // x2=x1+144
imageline(\$im,409,253,409,20,\$text_color); // y2=y1-233
imageline(\$im,409,20,32,20,\$text_color); // x2=x1-377
imageline(\$im,32,20,32,630,\$text_color); // y2=y1+610

## Re: having trouble using gd imageline in foreach loop

In article

Yeah, I bet you are.

[...]

If \$direction was "north" before executing that statement, what value does it
have now? And what will happen when you execute the next statement?

No matter whether \$direction was "north", "south", "east", or "west" *before*
executing this block of four statements, it's guaranteed to be "north" *after*
executing them.

## Re: having trouble using gd imageline in foreach loop

On 03/03/11 00:08, Lwangaman wrote:

// start, direction is north

// direction is north, so match this if clause

// now direction is west, so natch this if clause

// now direction is south, so match this if clause

// now direction is east, so match this if clause

// now direction is north, and we're going to start the loop again

Your if tree should use else if (or switch / case, but that's another
argument):

if (\$dir == "n") { /* do the north line, set \$dir to w */ }
else if (\$dir == "w") { /* do the west line, set \$dir to s */ }
else if (\$dir == "s") { /* do the south line, set \$dir to e */ }
else if (\$dir == "e") { /* do the east line, set \$dir to n */ }

Rgds

Denis McMahon

## Re: having trouble using gd imageline in foreach loop

On 03/03/11 00:08, Lwangaman wrote:

Oh yes, and:

should be either:

\$fibsequence = array(1,1,2,3,5,8,13,21,34,55,89,144,233,377,610);

or:

\$fibsequence[]=1;
\$fibsequence[]=1;
\$fibsequence[]=2;
\$fibsequence[]=3;
\$fibsequence[]=5;
\$fibsequence[]=8;
\$fibsequence[]=13;
\$fibsequence[]=21;
\$fibsequence[]=34;
\$fibsequence[]=55;
\$fibsequence[]=55;
\$fibsequence[]=89;
\$fibsequence[]=144;
\$fibsequence[]=233;
\$fibsequence[]=377;
\$fibsequence[]=610;

see http://uk2.php.net/manual/en/language.types.array.php

Rgds

Denis McMahon

## Re: having trouble using gd imageline in foreach loop

hahaha ok I get it! ELSEIF. Thanks.

## Re: having trouble using gd imageline in foreach loop

Yes in fact I was not using that in my code, I made this mistake in making the
example... It should in fact be "\$fibsequence = array(1,1,2,3...)" but I am
building the array automatically with a fibonacci generator function that I
wrote.