Php 从JSON文件创建图像?

Php 从JSON文件创建图像?,php,json,image,image-processing,Php,Json,Image,Image Processing,可能重复: Pixels.JSON { "274:130":"000", "274:129":"000", "274:128":"000", "274:127":"000", "274:126":"000", "274:125":"000", } 从具有X、Y坐标和十六进制代码的JSON文件到基于数据生成图像的最佳方式是什么?假设您的意思是,在客户端网页/浏览器中:您可以创建HTML5画布,并根据JSON数据直接绘制;我想这会很慢,但会奏效 您的“最佳”选择可能是重新

可能重复:

Pixels.JSON

{
  "274:130":"000",
  "274:129":"000",
  "274:128":"000",
  "274:127":"000",
  "274:126":"000",
  "274:125":"000",
}

从具有X、Y坐标和十六进制代码的JSON文件到基于数据生成图像的最佳方式是什么?

假设您的意思是,在客户端网页/浏览器中:您可以创建HTML5画布,并根据JSON数据直接绘制;我想这会很慢,但会奏效


您的“最佳”选择可能是重新考虑为什么要在JSON对象中发送图像数据,但我猜这其中有一些未共享的上下文。

这里缺少的是
高度
宽度
,但如果您可以获得,则可以使用从像素生成图像

范例

$pixels = '{
      "274:130":"000",
      "274:129":"000",
      "274:128":"000",
      "274:127":"000",
      "274:126":"000",
      "274:125":"000"
    }';

$list = json_decode($pixels, true);

//#GENERATE MORE DATA
for($i = 0; $i < 10000; $i ++) {
    $list[mt_rand(1, 300) . ":" . mt_rand(1, 300)] = random_hex_color();
}

$h = 300;
$w = 300;

$gd = imagecreatetruecolor($h, $w);
// ImageFillToBorder($gd, 0, 0, 0, 255);

foreach ( $list as $xy => $color ) {
    list($r, $g, $b) = html2rgb($color);
    list($x, $y) = explode(":", $xy);
    $color = imagecolorallocate($gd, $r, $g, $b);
    imagesetpixel($gd, $x, $y, $color);
}

header('Content-Type: image/png');
imagepng($gd);

解码,使用循环,将十六进制数拆分,然后绘制像素或线条。是否要使用Json中的数据生成图像?例如,基于上述样本数据。生成一个显示000的图像。
function html2rgb($color) {
    if ($color[0] == '#')
        $color = substr($color, 1);
    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0] . $color[1],$color[2] . $color[3],$color[4] . $color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0] . $color[0],$color[1] . $color[1],$color[2] . $color[2]);
    else
        return false;
    return array(hexdec($r),hexdec($g),hexdec($b));
}

function random_hex_color(){
    return sprintf("%02X%02X%02X", mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
}