Hi all,
First post here and a php newbie so please be gentle!
I’m trying to learn php and have a script I’m trying to breakdown to understand some of the functionality (don’t worry the script is freely available and I’m just using it to learn!).
The script is taking an image and plotting certain points in the image. I am starting to get to grips with a lot of the script now, but there is a tricky bit I just can’t get my head around.
I’m just wondering if a kind soul here would be able to explain to me what the below is doing exactly? I have started to understand arrays - mainly from the php manual (but I think this is getting into multidimensional arrays which is a bit beyond me right now - I’m not even sure this is one!)
The script defines $pattern = array() then starts populating the array with co-ordinates (x and y) from an image that is loaded.
[php]for ($x = 0; $x < $numPages; $x++) //loop through width/columns
{
$colorAbove = “None”; //reset for beginning of a column
$changes = FALSE;
$numColorBands = -1;
for ($y = 0; $y < $imageheight; $y++)//loop through height
{
$currentColor = imagecolorat($image ,$x ,$y);
if ($colorAbove === "None" and $currentColor == 0)//if picture is black at the top border create start of a color band
{
$colorAbove = $currentColor;
$numColorBands += 1;
$pattern[$x][$numColorBands] = array("start"=>0);
$changes = TRUE;
}
elseif ($currentColor == 0 and $y == $imageheight-1) //create end mark if the bottom pixel is black
{
$pattern[$x][$numColorBands]["end"] = $y+1;
$changes = TRUE;
}
elseif ($colorAbove === "None" or $colorAbove == $currentColor)//do nothing at begin of column if the first pixel is white, or if color doesn't change
{
$colorAbove = $currentColor;
continue;
}
elseif ($colorAbove == 1 and $currentColor == 0) //create mark if color goes from white to black
{
$colorAbove = $currentColor;
$numColorBands += 1;
$pattern[$x][$numColorBands] = array("start"=>$y);
$changes = TRUE;
}
elseif ($colorAbove == 0 and $currentColor == 1) //create mark if color goes from black to white
{
$colorAbove = $currentColor;
$pattern[$x][$numColorBands]["end"] = $y+1;
$changes = TRUE;
}
}
if ($changes == FALSE)
{
$pattern[$x] = "nofolds"; //mark no folds, these will only be allowed at start or end of pattern
}
}
return ($pattern);[/php]
I’m understanding what we are doing with a loop here and where and why it is marking points based on pixel colours in the image, what I don’t understand is how it is storing these in the $pattern array.
Why do we have double, sometimes triple, [] when writing to the array? What does this do?
[php]$pattern[$x][$numColorBands] = array(“start”=>0);[/php]
[php]$pattern[$x][$numColorBands][“end”] = $y+1;[/php]
Please be assured that I have literally spent hours and hours trying to figure this out for myself but it’s becoming frustrating now that I don’t understand it!!! :o
Any help or advice would be truly appreciated!
Many thanks in advance
EB