我想从图像中获取RGB值的数组。例如。 (2 X 2 PIX例子。)PHP - JPEG图像到RGB值阵列
[[[R, G, B], [R, G, B]], [[R, G, B], [R, G, B]]]
我现在有代码:
<?php
// open an image
$image = imagecreatefromjpeg('image.jpg'); // imagecreatefromjpeg/png/
// get image dimension, define colour array
$width = imagesx($image);
$height = imagesy($image);
$colors = [];
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;
}
}
print_r($colors);
?>
上述不工作。 我现在的形象仅仅是一个2×2 PIX JPEG应该输出:
[[[0, 255, 0], [255, 0, 0]], [[0, 0, 255], [255, 255, 255]]]
任何帮助,不胜感激!
您不向''颜色'数组添加'$ r $ g $ b' ......我会从那里开始。 '$ colors [] = array($ r,$ g,$ b);' – cmorrissey
你忘了把$ r,$ g和$ b放到你的$ colors数组中吗?我没有看到你在上面的代码中做了什么。 – Will
是的。谢谢,已添加。现在只需要输出“Array()”。 – Tom