加载图像到数组,2位


PHP Load image into array, 2 bit

我想将整个图像(PNG)加载到一个二维数组中,其中黑色像素为true,白色像素为false

做这件事最有效的方法是什么?

我应该将图像转换为位图并尝试读取,还是有更有效的方法?

应该这样做:

$image = imagecreatefrompng("input.png");
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
for ($y = 0; $y < $height; $y++)
{
    for ($x = 0; $x < $width; $x++)
    {
        $rgb = imagecolorat($image, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        $black = ($r == 0 && $g == 0 && $b == 0);
        $colors[$x][$y] = $black;
    } 
}
使用Imagick::exportImagePixels()可能更有效。