All Products
Search
Document Center

PolarDB:AI_ImageAsBase64

Last Updated:Mar 28, 2026

Converts binary image data into a Base64-encoded string.

Syntax

text AI_ImageAsBase64(bytea image, boolean mime_type DEFAULT true)

Parameters

ParameterTypeDefaultDescription
imagebyteaRequiredThe image data in binary format.
mime_typebooleantrueSpecifies whether the output includes a MIME type prefix. Valid values: true (default) and false.

mime_type values:

  • true (default): The returned string starts with data: followed by the image's MIME type. For example: data:image/jpeg;base64,xxxxx, where xxxxx is the Base64-encoded image data.

  • false: The returned string contains only the encoded image data, with no MIME type prefix.

Note

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...