image-dimensions.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. export interface ImageDimensions {
  2. width: number;
  3. height: number;
  4. }
  5. /**
  6. * Read dimensions from the headers of image formats supported by the read tool.
  7. * This is intentionally a narrow header check, not a replacement for decoding:
  8. * it is used only to decide whether an original image is already safe to return
  9. * when the full image processor cannot be loaded.
  10. */
  11. export function readImageDimensions(bytes: Uint8Array, mimeType: string): ImageDimensions | undefined {
  12. const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
  13. switch (mimeType) {
  14. case "image/png":
  15. return readPngDimensions(buffer);
  16. case "image/jpeg":
  17. return readJpegDimensions(buffer);
  18. case "image/gif":
  19. return readGifDimensions(buffer);
  20. case "image/webp":
  21. return readWebpDimensions(buffer);
  22. default:
  23. return undefined;
  24. }
  25. }
  26. function readPngDimensions(buffer: Buffer): ImageDimensions | undefined {
  27. if (
  28. buffer.length < 24 ||
  29. !buffer.subarray(0, 8).equals(Buffer.from("89504e470d0a1a0a", "hex")) ||
  30. buffer.readUInt32BE(8) !== 13 ||
  31. buffer.toString("ascii", 12, 16) !== "IHDR"
  32. ) {
  33. return undefined;
  34. }
  35. return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) };
  36. }
  37. function readJpegDimensions(buffer: Buffer): ImageDimensions | undefined {
  38. if (buffer.length < 4 || buffer[0] !== 0xff || buffer[1] !== 0xd8) return undefined;
  39. const startOfFrameMarkers = new Set([0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf]);
  40. let offset = 2;
  41. while (offset + 8 < buffer.length) {
  42. if (buffer[offset] !== 0xff) {
  43. offset += 1;
  44. continue;
  45. }
  46. while (offset < buffer.length && buffer[offset] === 0xff) offset += 1;
  47. const marker = buffer[offset];
  48. if (marker === undefined || marker === 0xda || marker === 0xd9) return undefined;
  49. if (startOfFrameMarkers.has(marker)) {
  50. if (offset + 7 >= buffer.length || buffer.readUInt16BE(offset + 1) < 8) return undefined;
  51. return { width: buffer.readUInt16BE(offset + 6), height: buffer.readUInt16BE(offset + 4) };
  52. }
  53. if (marker === 0x01 || marker === 0xd8 || (marker >= 0xd0 && marker <= 0xd7)) {
  54. offset += 1;
  55. continue;
  56. }
  57. if (offset + 2 >= buffer.length) return undefined;
  58. const segmentLength = buffer.readUInt16BE(offset + 1);
  59. if (segmentLength < 2) return undefined;
  60. offset += segmentLength + 1;
  61. }
  62. return undefined;
  63. }
  64. function readGifDimensions(buffer: Buffer): ImageDimensions | undefined {
  65. if (buffer.length < 10) return undefined;
  66. const signature = buffer.toString("ascii", 0, 6);
  67. if (signature !== "GIF87a" && signature !== "GIF89a") return undefined;
  68. return { width: buffer.readUInt16LE(6), height: buffer.readUInt16LE(8) };
  69. }
  70. function readWebpDimensions(buffer: Buffer): ImageDimensions | undefined {
  71. if (buffer.length < 30 || buffer.toString("ascii", 0, 4) !== "RIFF" || buffer.toString("ascii", 8, 12) !== "WEBP") {
  72. return undefined;
  73. }
  74. const chunk = buffer.toString("ascii", 12, 16);
  75. if (chunk === "VP8 ") {
  76. if (!buffer.subarray(23, 26).equals(Buffer.from([0x9d, 0x01, 0x2a]))) return undefined;
  77. return {
  78. width: buffer.readUInt16LE(26) & 0x3fff,
  79. height: buffer.readUInt16LE(28) & 0x3fff,
  80. };
  81. }
  82. if (chunk === "VP8L") {
  83. if (buffer[20] !== 0x2f) return undefined;
  84. const bits = buffer.readUInt32LE(21);
  85. return { width: (bits & 0x3fff) + 1, height: ((bits >> 14) & 0x3fff) + 1 };
  86. }
  87. if (chunk === "VP8X") {
  88. return {
  89. width: buffer.readUIntLE(24, 3) + 1,
  90. height: buffer.readUIntLE(27, 3) + 1,
  91. };
  92. }
  93. return undefined;
  94. }