您可以使用netpbm的 pngtopnm 函数将 PNG 转换为易于解析的 PNM。这是一个有点幼稚的 php 脚本,可以帮助您获得所需的内容:
<?php
$pngFilePath = 'template.png';
// Get the raw results of the png to pnm conversion
$contents = shell_exec("pngtopnm $pngFilePath");
// Break the raw results into lines
// 0: P6
// 1: <WIDTH> <HEIGHT>
// 2: 255
// 3: <BINARY RGB DATA>
$lines = preg_split('/\n/', $contents);
// Ensure that there are exactly 4 lines of data
if(count($lines) != 4)
die("Unexpected results from pngtopnm.");
// Check that the first line is correct
$type = $lines[0];
if($type != 'P6')
die("Unexpected pnm file header.");
// Get the width and height (in an array)
$dimensions = preg_split('/ /', $lines[1]);
// Get the data and convert it to an array of RGB bytes
$data = $lines[3];
$bytes = unpack('C*', $data);
print_r($bytes);
?>