how to count number of pixels in image (php)

前端 未结 1 907
走了就别回头了
走了就别回头了 2021-01-22 12:12

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:



        
相关标签:
1条回答
  • 2021-01-22 12:49

    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].

    0 讨论(0)
提交回复
热议问题