2

Please help me to count number of pixels in image, or put out the array of RGB.

So this is the script thet give me one element from array:

<?php
    $img = "1.png";
    $imgHand = imagecreatefrompng("$img");
    $imgSize = GetImageSize($img);
    $imgWidth = $imgSize[0];
    $imgHeight = $imgSize[1];
    echo '<img src="'.$img.'"><br><br>';
    for ($l = 0; $l < $imgHeight; $l++) {
        for ($c = 0; $c < $imgWidth; $c++) {
            $pxlCor = ImageColorAt($imgHand,$c,$l);
            $pxlCorArr = ImageColorsForIndex($imgHand, $pxlCor);
        }
    }


        print_r($pxlCorArr); 
?>

sorry for my english i from ukraine

Bob Dylan
  • 91
  • 1
  • 9

1 Answers1

6

The number of pixels in an image is simply the height multiplied by the width.

However, I think this is what you want:

<?php
    $img = "1.png";
    $imgHand = imagecreatefrompng("$img");
    $imgSize = GetImageSize($img);
    $imgWidth = $imgSize[0];
    $imgHeight = $imgSize[1];
    echo '<img src="'.$img.'"><br><br>';

    // Define a new array to store the info
    $pxlCorArr= array();

    for ($l = 0; $l < $imgHeight; $l++) {
        // Start a new "row" in the array for each row of the image.
        $pxlCorArr[$l] = array();

        for ($c = 0; $c < $imgWidth; $c++) {
            $pxlCor = ImageColorAt($imgHand,$c,$l);

            // Put each pixel's info in the array
            $pxlCorArr[$l][$c] = ImageColorsForIndex($imgHand, $pxlCor);
        }
    }

    print_r($pxlCorArr); 
?>

This will store all the pixel data for the image in the pxlCor and pxlCorArr arrays, which you can then manipulate to output what you want.

The array is a 2d array, meaning you can refrence an individual pixel with an $pxlCorArr[y][x] starting at [0][0].

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • @VolodyaDaniliv I updated it to store the data in a 2d array so you can reference individual pixels with a sort of x,y coordinate in the array. Not sure if that's what you want but now that you know how it's done, I'm sure you can figure it out. – sachleen Oct 28 '12 at 23:24