( imageBuffer: Buffer, maxTokens: number, originalMediaType?: string, )
| 596 | * Converts tokens to bytes using the formula: maxBytes = (maxTokens / 0.125) * 0.75 |
| 597 | */ |
| 598 | export async function compressImageBufferWithTokenLimit( |
| 599 | imageBuffer: Buffer, |
| 600 | maxTokens: number, |
| 601 | originalMediaType?: string, |
| 602 | ): Promise<CompressedImageResult> { |
| 603 | // Convert token limit to byte limit |
| 604 | // base64 uses about 4/3 the original size, so we reverse this |
| 605 | const maxBase64Chars = Math.floor(maxTokens / 0.125) |
| 606 | const maxBytes = Math.floor(maxBase64Chars * 0.75) |
| 607 | |
| 608 | return compressImageBuffer(imageBuffer, maxBytes, originalMediaType) |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Compresses an image block to fit within a maximum byte size. |
no test coverage detected