( imageBlock: ImageBlockParam, )
| 443 | * Also returns dimension information for coordinate mapping |
| 444 | */ |
| 445 | export async function maybeResizeAndDownsampleImageBlock( |
| 446 | imageBlock: ImageBlockParam, |
| 447 | ): Promise<ImageBlockWithDimensions> { |
| 448 | // Only process base64 images |
| 449 | if (imageBlock.source.type !== 'base64') { |
| 450 | return { block: imageBlock } |
| 451 | } |
| 452 | |
| 453 | // Decode base64 to buffer |
| 454 | const imageBuffer = Buffer.from(imageBlock.source.data, 'base64') |
| 455 | const originalSize = imageBuffer.length |
| 456 | |
| 457 | // Extract extension from media type |
| 458 | const mediaType = imageBlock.source.media_type |
| 459 | const ext = mediaType?.split('/')[1] || 'png' |
| 460 | |
| 461 | // Resize if needed |
| 462 | const resized = await maybeResizeAndDownsampleImageBuffer( |
| 463 | imageBuffer, |
| 464 | originalSize, |
| 465 | ext, |
| 466 | ) |
| 467 | |
| 468 | // Return resized image block with dimension info |
| 469 | return { |
| 470 | block: { |
| 471 | type: 'image', |
| 472 | source: { |
| 473 | type: 'base64', |
| 474 | media_type: |
| 475 | `image/${resized.mediaType}` as Base64ImageSource['media_type'], |
| 476 | data: resized.buffer.toString('base64'), |
| 477 | }, |
| 478 | }, |
| 479 | dimensions: resized.dimensions, |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Compresses an image buffer to fit within a maximum byte size. |
no test coverage detected