php png的用法,php – 如何使用PNG的IDAT块?

使用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
\n";

echo "chunk crc :0x" . sprintf('%08x',$chunk_crc) . "h
\n";

echo "chunk type :$chunk_type
\n";

echo "chunk data $chunk_type bytes:
\n" . chunk_split(bin2hex($chunk_data)) . "
\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
\n";

$bytes_per_pixel = $depth / 8;

break;

case 'PLTE':

$palette = array();

for($i=0;$i

$tupl = bin2hex(substr($chunk_data,$i,3));

$palette[] = $tupl;

if($i && ($i % 30 == 0)) {

echo "
\n";

}

echo '[' . $tupl . ']';

}

echo print_r($palette,true) . "
";

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:
\n" . chunk_split(bin2hex($decompressed),2 + $width * $bytes_per_pixel * 2) . "
\n";

for($row=0; $row

for($col=1; $col<=$width; $col++) {

$index = (int)substr($decompressed,((int)$row*($width+1)+$col),1);

echo '' . $index . '';

}

echo "
\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 "


";

} while(($pos < $len) && --$safety);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值