Home > other >  PHP keeps skipping to 144, but when echoed, is correct value
PHP keeps skipping to 144, but when echoed, is correct value

Time:11-08

I'm currently trying to get all pixel data inside of an image, and return it in JSON after encoding an Array. However, when I try to insert the $y data into the array, it always inserts 144. No in-between, always 144. When I echo $y, however, I get "0, 1, 2, etc."

$x, $r, $g, and $b are correct.

Any ideas? Here's my code:

<?php
    header('Content-Type: application/json');
    
    class PixelData {
        private $ar = array(
            "pixeldata" => [
                
            ]
        );
        
        public function getPixel($x, $y, $im) {
            echo $y; // echoes "0, 1, 2, 3, etc."
            global $ar;
            $ar["pixeldata"][$x]["x"] = $x;
            $ar["pixeldata"][$x]["y"] = $y;
            $rgb = imagecolorat($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $ar["pixeldata"][$x]["r"] = $r;
            $ar["pixeldata"][$x]["g"] = $g;
            $ar["pixeldata"][$x]["b"] = $b;
        }
    }
    
    $src = "D:\Pictures\Test.png";
    $im = imagecreatefrompng($src);
    $size = getimagesize($src);
    $width = imagesx($im);
    $height = imagesy($im);
    
    for($x = 0; $x < $width; $x  )
    {
        for($y = 0; $y < $height; $y  )
        {
            $pd = new PixelData();
            $pd->getPixel($x, $y, $im);
        }
    }
    
    $js = json_encode($ar);
    
    echo $js;
?>

CodePudding user response:

To elaborate further on my comment:

Your array is basically one-dimensional, because the only variable aspect is $ar['pixeldata'][$x]; you're never adding $y as another dimension. So, every time you increment $y in the inner-most for loop to go to the next level of $height you're overwriting the previous $x values. Basically, by the time your script is done, pixeldata will only contain the data of the top-most row of pixels, therefore they're always 144.

I also didn't notice until now, but if you wanted to store the data within the PixelData private array, you can't create a new instance within the for loops, you'd need to do that outside of the for loops.

This is probably what you want. As you can see, I've added an extra dimension to you pixeldata array, so as to include both $x and $y dimensions.

<?php
    header('Content-Type: application/json');
    
    class PixelData {
        private $ar = array(
            "pixeldata" => [
                
            ]
        );
        
        public function getPixel($x, $y, $im) {
            echo $y; // echoes "0, 1, 2, 3, etc."

            // Instantiate an array for this pixel
            $this->ar["pixeldata"][$x][$y] = [];

            $this->ar["pixeldata"][$x][$y]["x"] = $x;
            $this->ar["pixeldata"][$x][$y]["y"] = $y;
            $rgb = imagecolorat($im, $x, $y);
            $r = ($rgb >> 16) & 0xFF;
            $g = ($rgb >> 8) & 0xFF;
            $b = $rgb & 0xFF;
            $this->ar["pixeldata"][$x][$y]["r"] = $r;
            $this->ar["pixeldata"][$x][$y]["g"] = $g;
            $this->ar["pixeldata"][$x][$y]["b"] = $b;
        }
        
        public function getAr() {
            return $this->ar;
        }
    }
    
    $src = "D:\Pictures\Test.png";
    $im = imagecreatefrompng($src);
    $size = getimagesize($src);
    $width = imagesx($im);
    $height = imagesy($im);

    $pd = new PixelData();
    
    for($x = 0; $x < $width; $x  )
    {
        for($y = 0; $y < $height; $y  )
        {
            $pd->getPixel($x, $y, $im);
        }
    }
    
    $js = json_encode($pd->getAr());
    
    echo $js;
?>
  •  Tags:  
  • php
  • Related