Class: Image
@providerprotocol/ai / @providerprotocol/ai / Image
Class: Image
Section titled “Class: 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.
Example
Section titled “Example”// Load from fileconst fileImage = await Image.fromPath('./photo.jpg');
// Reference by URLconst urlImage = Image.fromUrl('https://example.com/image.png');
// From raw bytesconst bytesImage = Image.fromBytes(uint8Array, 'image/png');
// Use in a messageconst message = new UserMessage([image.toBlock()]);Properties
Section titled “Properties”height?
Section titled “height?”
readonlyoptionalheight:number
Defined in: src/core/media/Image.ts:94
Image height in pixels, if known
mimeType
Section titled “mimeType”
readonlymimeType:string
Defined in: src/core/media/Image.ts:90
MIME type of the image (e.g., ‘image/jpeg’, ‘image/png’)
source
Section titled “source”
readonlysource:ImageSource
Defined in: src/core/media/Image.ts:88
The underlying image source (bytes, base64, or URL)
width?
Section titled “width?”
readonlyoptionalwidth:number
Defined in: src/core/media/Image.ts:92
Image width in pixels, if known
Accessors
Section titled “Accessors”hasData
Section titled “hasData”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”toBase64()
Section titled “toBase64()”toBase64():
string
Defined in: src/core/media/Image.ts:124
Converts the image to a base64-encoded string.
Returns
Section titled “Returns”string
The image data as a base64 string
Throws
Section titled “Throws”When the source is a URL (data must be fetched first)
toBlock()
Section titled “toBlock()”toBlock():
ImageBlock
Defined in: src/core/media/Image.ts:193
Converts this Image to an ImageBlock for use in UPP messages.
Returns
Section titled “Returns”An ImageBlock that can be included in message content arrays
toBytes()
Section titled “toBytes()”toBytes():
Uint8Array
Defined in: src/core/media/Image.ts:157
Gets the image data as raw bytes.
Returns
Section titled “Returns”Uint8Array
The image data as a Uint8Array
Throws
Section titled “Throws”When the source is a URL (data must be fetched first)
toDataUrl()
Section titled “toDataUrl()”toDataUrl():
string
Defined in: src/core/media/Image.ts:146
Converts the image to a data URL suitable for embedding in HTML or CSS.
Returns
Section titled “Returns”string
A data URL in the format data:{mimeType};base64,{data}
Throws
Section titled “Throws”When the source is a URL (data must be fetched first)
toUrl()
Section titled “toUrl()”toUrl():
string
Defined in: src/core/media/Image.ts:180
Gets the URL for URL-sourced images.
Returns
Section titled “Returns”string
The image URL
Throws
Section titled “Throws”When the source is not a URL
fromBase64()
Section titled “fromBase64()”
staticfromBase64(base64,mimeType):Image
Defined in: src/core/media/Image.ts:278
Creates an Image from a base64-encoded string.
Parameters
Section titled “Parameters”base64
Section titled “base64”string
The base64-encoded image data (without data URL prefix)
mimeType
Section titled “mimeType”string
The MIME type of the image
Returns
Section titled “Returns”Image
An Image containing the base64 data
Example
Section titled “Example”const image = Image.fromBase64(base64String, 'image/jpeg');fromBlock()
Section titled “fromBlock()”
staticfromBlock(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.
Parameters
Section titled “Parameters”An ImageBlock from message content
Returns
Section titled “Returns”Image
An Image with the block’s source and metadata
fromBytes()
Section titled “fromBytes()”
staticfromBytes(data,mimeType):Image
Defined in: src/core/media/Image.ts:262
Creates an Image from raw byte data.
Parameters
Section titled “Parameters”Uint8Array
The image data as a Uint8Array
mimeType
Section titled “mimeType”string
The MIME type of the image
Returns
Section titled “Returns”Image
An Image containing the byte data
Example
Section titled “Example”const image = Image.fromBytes(pngData, 'image/png');fromPath()
Section titled “fromPath()”
staticfromPath(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.
Parameters
Section titled “Parameters”string
Path to the image file
Returns
Section titled “Returns”Promise<Image>
Promise resolving to an Image with the file contents
Example
Section titled “Example”const image = await Image.fromPath('./photos/vacation.jpg');fromUrl()
Section titled “fromUrl()”
staticfromUrl(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.
Parameters
Section titled “Parameters”string
URL pointing to the image
mimeType?
Section titled “mimeType?”string
Optional MIME type override
Returns
Section titled “Returns”Image
An Image referencing the URL
Example
Section titled “Example”const image = Image.fromUrl('https://example.com/logo.png');