All Products
Search
Document Center

PolarDB:AI_ImageAsBase64

Last Updated:Apr 08, 2025

Converts binary image data into a Base64-encoded string.

Syntax

text AI_ImageAsBase64 (bytea image, boolean mime_type DEFAULT true)

Parameters

Parameter

Description

image

Image data in binary format.

mime_type

Specifies whether the output Base64 string includes the Multipurpose Internet Mail Extensions (MIME) type prefix. Valid values:

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

  • false: The returned Base64 string contains only the encoded image data, without any MIME type prefix.

Note

For more information about MIME types, see the related community documentation.

Return values

Returns a Base64-encoded string representation of the input image.

Description

  • This function currently supports the following image data formats: JPEG, PNG, and BMP.

  • In most cases, AI models have specific size requirements for input images. However, images in their original form may have high pixel dimensions, which can exceed the acceptable size for processing by the AI model. In this case, you can use the AI_ResizeImage function to resize the image to the required dimensions before using this function to convert it into a Base64 string.

Example

SELECT AI_ImageAsBase64(data) FROM ai_image_test WHERE id = 1;

Sample output:

---
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD...