如果你正試圖從那些已經被創建,或者如果你沒有訪問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