我有这段代码,我试图将图像像素值存储在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);
答案 0 :(得分:1)
您所说的数组就是行,您需要构建每行,然后将其添加到行列表中
$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;
}
或执行与重新创建图像并使用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);
}
}