如何计算图像中的像素数(php)

Bob*_*lan 2 php

请帮我计算图像中的像素数,或者输出RGB数组.

所以这是脚本给我一个来自数组的元素:

<?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); 
?>
Run Code Online (Sandbox Code Playgroud)

对不起我来自乌克兰的英语

sac*_*een 5

图像中的像素数量就是高度乘以宽度.

但是,我认为这就是你想要的:

<?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); 
?>
Run Code Online (Sandbox Code Playgroud)

这将存储pxlCorpxlCorArr数组中图像的所有像素数据,然后您可以操作它们以输出您想要的内容.

该数组是一个二维数组,这意味着你可以用一个$pxlCorArr[y][x]开头的参考来引用一个像素[0][0].