如果你正试图从那些已经被创建,或者如果你没有访问Imagick库图像删除tIME
块,我测试了一些代码,可以从小型十岁上下的* PNG删除块文件。
PNG文件具有specific format:
- PNG签名[8个字节]
- 块数据 - 与IHDR开始并以IEND块在它们之间的其它的块结束。每个块具有以下:
- 长度[4个字节]
- 类型[4个字节]
- 数据[长度字节](可能是空的,如果长度为0)
- CRC(循环冗余码) [4个字节]
利用这些信息,我们就可以通过块逐一念给我们找到要删除的一个。
// read the PNG file's contents into a string
$str = file_get_contents('hD0aZ.png');
// how far into the string we are currently
$offset = 0;
// offset for the PNG signature (8 bytes)
$offset += 8;
// what we will replace in the original string once it is found
$timechunk = '';
while($offset < strlen($str)){
// get the current chunk information
list($length,$chunk,$name) = readchunk(substr($str,$offset));
if($name == 'tIME'){
$timechunk = $chunk;
break;
}
// move to the next chunk
$offset += $length;
}
header("Content-Type:image/png");
print str_replace($timechunk,'',$str);
function readchunk($str){
$length = hexdec(bin2hex(substr($str,0,4)));
// 4 bytes for length, 4 bytes for chunk name, 4 bytes for CRC = 12 bytes
return array(
12+$length, // total length of chunk
substr($str,0,12+$length), // full chunk data
substr($str,4,4) // chunk name
);
}
*如果您的PNG文件过大,tIME
块不够早在字符串中,你可能最终与问题,如果你的$offset
尝试走了过来PHP_INT_MAX
。但是,我可以让这段代码处理你在问题中提供的图像。
在上面一行中,您突出显示的是'tIME'块的开始,这应该是最后一次修改时间([请参阅此处的规格](https://www.w3.org/TR/PNG/# 11tIME))。你也可以找到[这个Unix SE问题](https://unix.stackexchange.com/questions/255252/create-the-same-png-with-imagemagick-2-times-binaries-differ)有帮助。 –
@JoshH为什么只在Linux操作系统中添加?那么,如何使用Imagick(PHP)保存PNG时如何排除此块? – Codemole
由于不同的字节每次都处于已知的偏移量,因此可以选择添加后处理步骤来清零这些字节? – GeorgeQ