@wordpress/upload-media
Version:
Core media upload logic.
80 lines (79 loc) • 2.29 kB
JavaScript
// packages/upload-media/src/get-image-dimensions.ts
var MAX_HEADER_BYTES = 512 * 1024;
function parseJpeg(view) {
let offset = 2;
while (offset < view.byteLength) {
if (view.getUint8(offset) !== 255) {
return null;
}
while (offset < view.byteLength && view.getUint8(offset) === 255) {
offset++;
}
if (offset >= view.byteLength) {
return null;
}
const marker = view.getUint8(offset);
offset++;
if (marker === 1 || marker >= 208 && marker <= 217) {
continue;
}
if (marker === 218) {
return null;
}
if (offset + 2 > view.byteLength) {
return null;
}
const segmentLength = view.getUint16(offset);
if (segmentLength < 2) {
return null;
}
const isStartOfFrame = marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204;
if (isStartOfFrame) {
if (offset + 7 > view.byteLength) {
return null;
}
const height = view.getUint16(offset + 3);
const width = view.getUint16(offset + 5);
const interlaced = marker === 194 || marker === 198 || marker === 202 || marker === 206;
return { width, height, interlaced };
}
offset += segmentLength;
}
return null;
}
function parsePng(view) {
if (view.byteLength < 29) {
return null;
}
const isIhdr = view.getUint8(12) === 73 && // I
view.getUint8(13) === 72 && // H
view.getUint8(14) === 68 && // D
view.getUint8(15) === 82;
if (!isIhdr) {
return null;
}
const width = view.getUint32(16);
const height = view.getUint32(20);
const interlaceMethod = view.getUint8(28);
return { width, height, interlaced: interlaceMethod !== 0 };
}
async function getImageDimensions(file) {
try {
const headerBytes = Math.min(file.size, MAX_HEADER_BYTES);
const buffer = await file.slice(0, headerBytes).arrayBuffer();
const view = new DataView(buffer);
if (view.byteLength >= 3 && view.getUint16(0) === 65496) {
return parseJpeg(view);
}
if (view.byteLength >= 8 && view.getUint32(0) === 2303741511 && view.getUint32(4) === 218765834) {
return parsePng(view);
}
return null;
} catch {
return null;
}
}
export {
getImageDimensions
};
//# sourceMappingURL=get-image-dimensions.mjs.map