( base64Data: string, mimeType: ImageMimeType )
| 102 | } |
| 103 | |
| 104 | export function getImageDimensions( |
| 105 | base64Data: string, |
| 106 | mimeType: ImageMimeType |
| 107 | ): { width: number; height: number } | null { |
| 108 | try { |
| 109 | const buffer = Buffer.from(base64Data, "base64"); |
| 110 | |
| 111 | if (mimeType === "image/png") { |
| 112 | const png = PNG.sync.read(buffer); |
| 113 | return { width: png.width, height: png.height }; |
| 114 | } |
| 115 | |
| 116 | if (mimeType === "image/jpeg") { |
| 117 | const decoded = jpeg.decode(buffer, { useTArray: true }); |
| 118 | return { width: decoded.width, height: decoded.height }; |
| 119 | } |
| 120 | |
| 121 | if (mimeType === "image/gif") { |
| 122 | if (buffer.length >= 10) { |
| 123 | const width = buffer.readUInt16LE(6); |
| 124 | const height = buffer.readUInt16LE(8); |
| 125 | return { width, height }; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (mimeType === "image/webp") { |
| 130 | if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF") { |
| 131 | const webpType = buffer.toString("ascii", 8, 12); |
| 132 | if (webpType === "WEBP") { |
| 133 | if (buffer.toString("ascii", 12, 16) === "VP8 ") { |
| 134 | const width = buffer.readUInt16LE(26) & 0x3fff; |
| 135 | const height = buffer.readUInt16LE(28) & 0x3fff; |
| 136 | return { width, height }; |
| 137 | } |
| 138 | if (buffer.toString("ascii", 12, 16) === "VP8L") { |
| 139 | const bits = buffer.readUInt32LE(21); |
| 140 | const width = (bits & 0x3fff) + 1; |
| 141 | const height = ((bits >> 14) & 0x3fff) + 1; |
| 142 | return { width, height }; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return null; |
| 149 | } catch { |
| 150 | return null; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | export function calculateImageTokensFromBase64( |
| 155 | base64Data: string, |
no outgoing calls
no test coverage detected