Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将图像像素值存储在2D数组中,然后使用循环访问它们_Php_Loops_Multidimensional Array_Gd - Fatal编程技术网

使用PHP将图像像素值存储在2D数组中,然后使用循环访问它们

使用PHP将图像像素值存储在2D数组中,然后使用循环访问它们,php,loops,multidimensional-array,gd,Php,Loops,Multidimensional Array,Gd,我有一段代码,我尝试在2D数组中存储图像像素值,然后尝试访问它们,这样我就可以从数组中存储的像素重新创建相同的图像,下面是我尝试做的,但它只访问一维数组,任何能帮上忙的人都会非常感激 $resource = imagecreatefromjpeg("Broadway_tower_edit.jpg"); $width = 3; $height = 3; $arrayPixels = array(); //put pixels values in an array for($x = 0; $x

我有一段代码,我尝试在2D数组中存储图像像素值,然后尝试访问它们,这样我就可以从数组中存储的像素重新创建相同的图像,下面是我尝试做的,但它只访问一维数组,任何能帮上忙的人都会非常感激

$resource = imagecreatefromjpeg("Broadway_tower_edit.jpg");
$width = 3;
$height = 3;

$arrayPixels = array();

//put pixels values in an array

for($x = 0; $x < $width; $x++) {

    for($y = 0; $y < $height; $y++) {
        // pixel color at (x, y)
        $color = imagecolorat($resource, $x, $y);



        $arrayPixels1 = array("$color");

       //$myArray[$x][$y] = array('item' => "$color");
        $arrayPixels[] = $arrayPixels1;

    }

}
//access pixel values an try to create a image

$img = imagecreatetruecolor($width, $height);


for ($y = 0; $y < $height; ++$y) {
    for ($x = 0; $x < $width; ++$x) {
        imagesetpixel($img, $x, $y, $arrayPixels[$y][$x]);
    }
}

// Dump the image to the browser
header('Content-Type: image/jpg');
imagejpeg($img);

// Clean up after ourselves
imagedestroy($img);
$resource=imagecreatefromjpeg(“百老汇•塔•编辑.jpg”);
$width=3;
$height=3;
$arrayPixels=array();
//将像素值放入数组中
对于($x=0;$x<$width;$x++){
对于($y=0;$y<$height;$y++){
//(x,y)处的像素颜色
$color=imagecolorat($resource,$x,$y);
$arrayPixels1=数组($color”);
//$myArray[$x][$y]=数组('item'=>“$color”);
$arrayPixels[]=$arrayPixels1;
}
}
//访问像素值并尝试创建图像
$img=ImageCreateTureColor($width,$height);
对于($y=0;$y<$height;++$y){
对于($x=0;$x<$width;++$x){
imagesetpixel($img,$x,$y,$arrayPixels[$y][$x]);
}
}
//将图像转储到浏览器
标题('Content-Type:image/jpg');
图像jpeg($img);
//打扫干净
图像处理(img);

正如您所说,您的数组就是行,您需要构建每一行,然后将其添加到行列表中

$arrayPixels = array();
//put pixels values in an array
for($x = 0; $x < $width; $x++) {
    $row = array();
    for($y = 0; $y < $height; $y++) {
        // pixel color at (x, y)
        $row[] = imagecolorat($resource, $x, $y);

    }
    $arrayPixels[] = $row;
}
$arrayPixels=array();
//将像素值放入数组中
对于($x=0;$x<$width;$x++){
$row=array();
对于($y=0;$y<$height;$y++){
//(x,y)处的像素颜色
$row[]=imagecolorat($resource,$x,$y);
}
$arrayPixels[]=$row;
}
或者在重新创建图像并使用x和y坐标时执行相同的操作

//put pixels values in an array
for($x = 0; $x < $width; $x++) {
    for($y = 0; $y < $height; $y++) {
        // pixel color at (x, y)
       $arrayPixels[$y][$x] = imagecolorat($resource, $x, $y);

    }
}    
//将像素值放入数组中
对于($x=0;$x<$width;$x++){
对于($y=0;$y<$height;$y++){
//(x,y)处的像素颜色
$arrayPixels[$y][$x]=imagecolorat($resource,$x,$y);
}
}    

非常感谢@Nigel Ren,现在我可以在2D中访问索引值,但另一个问题是它会创建一个黑色图像,而不是我从中提取索引值的图像,您或任何人也能帮我吗?有几件事需要检查,请尝试使用原始图像显示图像(
$resource
)看看它是否正确显示。使用第二个版本(使用$x和$y),比较第一个图像和第二个图像中的像素,看看它们是否被正确复制。