Extract width/height from PNG or JPEG buffer
(buf: Buffer, ext: string)
| 10 | |
| 11 | /** Extract width/height from PNG or JPEG buffer */ |
| 12 | function getImageSize(buf: Buffer, ext: string): { width: number; height: number } | null { |
| 13 | try { |
| 14 | if (ext === "png" && buf.length > 24) { |
| 15 | // PNG: width at offset 16, height at offset 20 (big-endian uint32) |
| 16 | const width = buf.readUInt32BE(16) |
| 17 | const height = buf.readUInt32BE(20) |
| 18 | return { width, height } |
| 19 | } |
| 20 | if ((ext === "jpg" || ext === "jpeg") && buf.length > 2) { |
| 21 | // Validate JPEG magic bytes |
| 22 | if (buf[0] !== 0xFF || buf[1] !== 0xD8) return null |
| 23 | // JPEG: scan for SOF0 (0xFFC0) or SOF2 (0xFFC2) marker |
| 24 | let offset = 2 |
| 25 | while (offset < buf.length - 8) { |
| 26 | if (buf[offset] !== 0xFF) break |
| 27 | const marker = buf[offset + 1] |
| 28 | if (marker === 0xC0 || marker === 0xC2) { |
| 29 | const height = buf.readUInt16BE(offset + 5) |
| 30 | const width = buf.readUInt16BE(offset + 7) |
| 31 | return { width, height } |
| 32 | } |
| 33 | const segLen = buf.readUInt16BE(offset + 2) |
| 34 | offset += 2 + segLen |
| 35 | } |
| 36 | } |
| 37 | if (ext === "gif" && buf.length > 10) { |
| 38 | // GIF: width at offset 6, height at offset 8 (little-endian uint16) |
| 39 | const width = buf.readUInt16LE(6) |
| 40 | const height = buf.readUInt16LE(8) |
| 41 | return { width, height } |
| 42 | } |
| 43 | } catch {} |
| 44 | return null |
| 45 | } |
| 46 | |
| 47 | function resolveClientId( |
| 48 | server: ReactotronServer, |