php - 如何使用 PNG 的 IDAT block ?

标签 php png chunks

我试图了解数据是如何存储到 IDAT block 中的。我正在编写一个小 PHP 类,我可以检索大部分块信息,但我从 IDAT 获得的信息与我的图像像素不匹配:

enter image description here它是带 alpha(位深度 8)的 2×2px 真彩色。

但是当我这样解释 IDAT 数据时:

current(unpack('H*',gzuncompress($idat_data)));

我明白了

00000000ffffff00ffffff000000

我不明白它如何匹配像素。还是我的代码损坏了数据?

感谢您的帮助!

编辑:我明白了

08d705c101010000008010ff4f1750a93029e405fb

因为是16进制压缩数据,解压后好像少了几个字节

enter image description here

最佳答案

使用 gzinflate 但先跳过前 2 个字节和后 4 个字节。

$contents = file_get_contents($in_filename);
$pos = 8; // skip header

$color_types = array('Greyscale','unknown','Truecolour','Indexed-color','Greyscale with alpha','unknown','Truecolor with alpha');
$len = strlen($contents);
$safety = 1000;
do {
    list($unused,$chunk_len) = unpack('N', substr($contents,$pos,4));

    $chunk_type = substr($contents,$pos+4,4);

    $chunk_data = substr($contents,$pos+8,$chunk_len);

    list($unused,$chunk_crc) = unpack('N', substr($contents,$pos+8+$chunk_len,4));
    echo "chunk length:$chunk_len(dec) 0x" . sprintf('%08x',$chunk_len) . "h<br>\n";
    echo "chunk crc   :0x" . sprintf('%08x',$chunk_crc) . "h<br>\n";
    echo "chunk type  :$chunk_type<br>\n";
    echo "chunk data  $chunk_type bytes:<br>\n"  . chunk_split(bin2hex($chunk_data)) . "<br>\n";
    switch($chunk_type) {
        case 'IHDR':
        list($unused,$width,$height) = unpack('N2', substr($chunk_data,0,8));
        list($unused,$depth,$Color_type,$Compression_method,$Filter_method,$Interlace_method) = unpack('C*', substr($chunk_data,8));
        echo "Width:$width,Height:$height,depth:$depth,Color_type:$Color_type(" . $color_types[$Color_type] . "),Compression_method:$Compression_method,Filter_method:$Filter_method,Interlace_method:$Interlace_method<br>\n";
        $bytes_per_pixel = $depth / 8;
        break;

        case 'PLTE':
        $palette = array();
        for($i=0;$i<$chunk_len;$i+=3) {
            $tupl = bin2hex(substr($chunk_data,$i,3));
            $palette[] = $tupl;
            if($i && ($i % 30 == 0)) {
                echo "<br>\n";
            }
            echo '<span style="color:' . $tupl . ';">[' . $tupl . ']</span>';
        }
        echo print_r($palette,true) . "<br>";
        break;

        case 'IDAT':
        $compressed = substr($chunk_data,2,$chunk_len - 6); // 2 bytes on the front and 4 at the end
        $decompressed = gzinflate($compressed);
        echo "decompressed chunk data " . strlen($decompressed) . " bytes:<br>\n"  . chunk_split(bin2hex($decompressed),2 + $width * $bytes_per_pixel * 2) . "<br>\n";
        for($row=0; $row<$height; $row++) {
            for($col=1; $col<=$width; $col++) {
                $index = (int)substr($decompressed,((int)$row*($width+1)+$col),1);
                echo '<span style="color:' . $palette[$index] . ';">' . $index . '</span>';
            }
            echo "<br>\n";
        }
        // TODO use filters described here:
        // http://www.w3.org/TR/PNG/#9Filters
        // first byte of scan line is filter type
        break;

    }
    $pos += $chunk_len + 12;
    echo "<hr>";
} while(($pos < $len) && --$safety);

关于php - 如何使用 PNG 的 IDAT block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7292872/

相关文章:

PHP:未知语法可能是一个没有括号的函数?

php - MySQL - 如何连接两个或多个具有多个 ID 的表?

php - 如何对 codeigniter 数据库类中的列求和?

actionscript-3 - as3 |如何使用 Adob​​e AIR 导出 PNG

c# - 如何更快地加载/卸载图 block block (C#)

php - MySQL 使用数组查询多个表 (PHP)

c - Mac OS X 上的原生 PNG 库 (mavericks)

Pygame碰撞检测,透明边框

Python Flask block 数据上传不起作用

api - 有没有办法通过SoundCloud javascript流API强制使用MP3 block ?