[英]Load PNG and read pixels in PHP without GD?
我需要从真彩色 PNG 文件中读取准确的未更改像素数据 (ARGB),最好是从 PHP 中读取。
不幸的是,PHP 中的 GD 库与 alpha 通道混淆(将其从 8 位减少到 7 位),使其无法使用。
我目前假设我的选择是:
不过,我很想听听任何其他想法,或者对另一种方式的建议......
编辑:我认为#1已经出局了。 我尝试将 IDAT 数据 stream 传递给 gzinflate(),但它只是给了我一个数据错误。 (在 PHP 之外使用完全相同的数据执行完全相同的操作会产生预期的结果。)
ImageMagick怎么样?
<?php
$im = new Imagick("foo.png");
$it = $im->getPixelIterator();
foreach($it as $row => $pixels) {
foreach ($pixels as $column => $pixel) {
// Do something with $pixel
}
$it->syncIterator();
}
?>
您可以使用netpbm的 pngtopnm function 将 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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.