( imageBuffer: Buffer, maxTokens: number, originalMediaType?: string, )
| 581 | * Converts tokens to bytes using the formula: maxBytes = (maxTokens / 0.125) * 0.75 |
| 582 | */ |
| 583 | export async function compressImageBufferWithTokenLimit( |
| 584 | imageBuffer: Buffer, |
| 585 | maxTokens: number, |
| 586 | originalMediaType?: string, |
| 587 | ): Promise<CompressedImageResult> { |
| 588 | // Convert token limit to byte limit |
| 589 | // base64 uses about 4/3 the original size, so we reverse this |
| 590 | const maxBase64Chars = Math.floor(maxTokens / 0.125) |
| 591 | const maxBytes = Math.floor(maxBase64Chars * 0.75) |
| 592 | |
| 593 | return compressImageBuffer(imageBuffer, maxBytes, originalMediaType) |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Compresses an image block to fit within a maximum byte size. |
no test coverage detected