Converts binary image data into a Base64-encoded string.
Syntax
text AI_ImageAsBase64(bytea image, boolean mime_type DEFAULT true)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
image | bytea | Required | The image data in binary format. |
mime_type | boolean | true | Specifies whether the output includes a MIME type prefix. Valid values: true (default) and false. |
mime_type values:
true(default): The returned string starts withdata:followed by the image's MIME type. For example:data:image/jpeg;base64,xxxxx, wherexxxxxis the Base64-encoded image data.false: The returned string contains only the encoded image data, with no MIME type prefix.
For more information about MIME types, see MDN Web Docs: Data URLs.
Return value
Returns a text value containing the Base64-encoded representation of the input image.
Usage notes
Supported image formats: JPEG, PNG, and BMP.
AI models typically require images within a specific size range. If an image has large pixel dimensions that exceed an AI model's input limit, use AI_ResizeImage to resize it before calling this function.
Example
Convert a binary image stored in the ai_image_test table to a Base64-encoded string:
SELECT AI_ImageAsBase64(data) FROM ai_image_test WHERE id = 1;Sample output:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...