Skip to content

Class: Image

@providerprotocol/ai


@providerprotocol/ai / @providerprotocol/ai / Image

Defined in: src/core/media/Image.ts:86

Represents an image that can be used in UPP messages.

Images can be created from various sources (files, URLs, bytes, base64) and converted to different formats as needed by providers. The class provides a unified interface regardless of the underlying source type.

// Load from file
const fileImage = await Image.fromPath('./photo.jpg');
// Reference by URL
const urlImage = Image.fromUrl('https://example.com/image.png');
// From raw bytes
const bytesImage = Image.fromBytes(uint8Array, 'image/png');
// Use in a message
const message = new UserMessage([image.toBlock()]);

readonly optional height: number

Defined in: src/core/media/Image.ts:94

Image height in pixels, if known


readonly mimeType: string

Defined in: src/core/media/Image.ts:90

MIME type of the image (e.g., ‘image/jpeg’, ‘image/png’)


readonly source: ImageSource

Defined in: src/core/media/Image.ts:88

The underlying image source (bytes, base64, or URL)


readonly optional width: number

Defined in: src/core/media/Image.ts:92

Image width in pixels, if known

get hasData(): boolean

Defined in: src/core/media/Image.ts:114

Whether this image has data loaded in memory.

Returns false for URL-sourced images that reference external resources. These must be fetched before their data can be accessed.

boolean

toBase64(): string

Defined in: src/core/media/Image.ts:124

Converts the image to a base64-encoded string.

string

The image data as a base64 string

When the source is a URL (data must be fetched first)


toBlock(): ImageBlock

Defined in: src/core/media/Image.ts:193

Converts this Image to an ImageBlock for use in UPP messages.

ImageBlock

An ImageBlock that can be included in message content arrays


toBytes(): Uint8Array

Defined in: src/core/media/Image.ts:157

Gets the image data as raw bytes.

Uint8Array

The image data as a Uint8Array

When the source is a URL (data must be fetched first)


toDataUrl(): string

Defined in: src/core/media/Image.ts:146

Converts the image to a data URL suitable for embedding in HTML or CSS.

string

A data URL in the format data:{mimeType};base64,{data}

When the source is a URL (data must be fetched first)


toUrl(): string

Defined in: src/core/media/Image.ts:180

Gets the URL for URL-sourced images.

string

The image URL

When the source is not a URL


static fromBase64(base64, mimeType): Image

Defined in: src/core/media/Image.ts:278

Creates an Image from a base64-encoded string.

string

The base64-encoded image data (without data URL prefix)

string

The MIME type of the image

Image

An Image containing the base64 data

const image = Image.fromBase64(base64String, 'image/jpeg');

static fromBlock(block): Image

Defined in: src/core/media/Image.ts:291

Creates an Image from an existing ImageBlock.

Useful for converting content blocks received from providers back into Image instances for further processing.

ImageBlock

An ImageBlock from message content

Image

An Image with the block’s source and metadata


static fromBytes(data, mimeType): Image

Defined in: src/core/media/Image.ts:262

Creates an Image from raw byte data.

Uint8Array

The image data as a Uint8Array

string

The MIME type of the image

Image

An Image containing the byte data

const image = Image.fromBytes(pngData, 'image/png');

static fromPath(path): Promise<Image>

Defined in: src/core/media/Image.ts:217

Creates an Image by reading a file from disk.

The file is read into memory as bytes. MIME type is automatically detected from the file extension.

string

Path to the image file

Promise<Image>

Promise resolving to an Image with the file contents

const image = await Image.fromPath('./photos/vacation.jpg');

static fromUrl(url, mimeType?): Image

Defined in: src/core/media/Image.ts:245

Creates an Image from a URL reference.

The URL is stored as a reference and not fetched. Providers will handle URL-to-data conversion if needed. MIME type is detected from the URL path if not provided.

string

URL pointing to the image

string

Optional MIME type override

Image

An Image referencing the URL

const image = Image.fromUrl('https://example.com/logo.png');