我使用fopen()函數與圖像文件的二進制讀取參數(「RB」),如下圖所示:神祕地刪除字節PHP fopen()函數
$imageURL = "assets/" . $gameObject . "Assets" . "/" . $gameObject . $description . "Base8SmallPNG.png";
$imageExposed = fopen($imageURL, "rb");
當圖像被讀取,坐在文件指針的字符(或多個)將它們轉換爲十六進制後迴盪:
while (fgetc($imageExposed) !== false) {
$binaryString = fgetc($imageExposed);
echo bin2hex($binaryString);
}
不知何故,這個過程被丟棄每第二字節 - 例如,第一八位位組爲50 47 0A 0A(PG < LF > < LF> in ASCII)而不是89 50 4E 47(.PNG)。有什麼可以做到這一點?
在一個可能相關的說明中,圖像在處理之後從〜4415B到4526膨脹 - 儘管如上所述丟棄了數據。儘管包含圖像位圖(在IDAT和IEND之間)的段在我的位編輯器(Frhed)中爲980B,但在處理後(IDAT近似爲DT [44 54]),它不可避免地擴展到1554B,而不影響數據回顯的總量。
背景:圖像是一個4.4KB的PNG;它正在按照PHP中的描述進行處理,然後通過以下AJAX調用接收到回顯數據,然後將其推送到控制檯; 4454之後的數據(代表IDAT塊)被推送到一個Uint8ClampedArray中顯示 - 或者,如果這些字節沒有被丟棄,它將會被顯示。數據仍然被髮送到數組,但它不會被轉換爲進程的十進制數,或者通過ImageData推送到畫布元素。
AFAIK Uint8ClampedArray需要十進制數據才能工作,所以我寫了一個十六進制/十進制轉換器。由於問題在原始的AJAX響應中出現,因此顯示小數點數據或ImageData表示是沒有意義的,所以這就是爲什麼我將這些數據包含在內,但是阻止它們生成輸出的原因。
function imageAnalysis(dynamicObject, gameObjectDesc) {
phpCourier = new XMLHttpRequest();
phpCourier.onreadystatechange = function() {
if (phpCourier.readyState === 4 && phpCourier.status === 200) {
var imageParserTest = document.createElement('canvas'),
stringBox = [],
base = 16,
hexNumbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"],
output,
stringBoxCleaner,
dimX = 24,
dimY = 47,
channels = "rgba",
colourMap = [],
colourStep = 0,
chunkString,
bitMapping;
imageParserTest.style.zIndex = 90000;
for (bitCounter = 0; bitCounter < phpCourier.response.length; bitCounter += 1) {
stringBox.push(phpCourier.response.substr(bitCounter, 1));
stringBox.push(phpCourier.response.substr(bitCounter + 1, 1));
colourMap.push(channels.substr(colourStep, 1));
if (colourStep !== channels.length) {
colourStep += 1;
}
if (colourStep === channels.length) {
colourStep = 0;
}
/*if (stringBox[0] + stringBox[1] > 128) {
if (colourMap[bitCounter] === "a") {
output = 255;
} else if (colourMap[bitCounter] === "b") {
output = 255;
} else if (colourMap[bitCounter] === "g") {
output = 255;
} else if (colourMap[bitCounter] === "r") {
output = 255;
}
} else {
if (colourMap[bitCounter] === "a") {
output = 255;
} else if (colourMap[bitCounter] === "b") {
output = 0;
} else if (colourMap[bitCounter] === "g") {
output = 0;
} else if (colourMap[bitCounter] === "r") {
output = 0;
}
}*/
output = stringBox[0] + stringBox[1];
//console.log(phpCourier.response.substr(bitCounter, 8));
//console.log(phpCourier.response);
//Uint8ClampedArray[bitCounter] = output;
if (phpCourier.response.substr(bitCounter, 4) === "4454") {//"49444154") {
Uint8ClampedArray = new Uint8ClampedArray(phpCourier.response.length - bitCounter - 9);
bitMapping = true;
console.log("bah");
}
if (bitMapping === true) {
Uint8ClampedArray[(phpCourier.response.length - bitCounter - 9)] = output;
console.log(phpCourier.response.substr(bitCounter, 2));
}
stringBox = [];
}
console.log(Uint8ClampedArray);
console.log("Base Data Length " + phpCourier.response.length);
console.log("Required Dimensions " + dimX * dimY * 4);
console.log("Output Length " + Uint8ClampedArray.length);
//pipedImage = new ImageData(Uint8ClampedArray, dimX, dimY);
//document.body.appendChild(imageParserTest);
//imageParserTest.getContext("2d").putImageData(pipedImage, dimX, dimY);
//console.log(pipedImage);
//console.log(imageParserTest.getContext("2d").getImageData(39, 66, 39, 66));
console.log(phpCourier.response.length);
console.log(phpCourier.response);
}
};
phpCourier.open("POST", "bitManip.php", true);
phpCourier.send("gameObject=" + dynamicObject + "&description=" + gameObjectDesc);
}
imageAnalysis(aeolus, "Basic");
你打電話龜etc()兩次對於每個循環 –
是的,是的,我是(或是)。 我跟着你的第二個選項(!feof而不是檢查fgetc本身),數據在經過數週的混淆後可以完美地打印出來,這些東西可能會讓事情變得糟糕。感謝您的幫助:) –