> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagewritebase64.md).

# ImageWriteBase64

## Syntax

```
ImageWriteBase64( name [, format] )
```

Or as a member:

```
someImage.toBase64String( [format] )
```

## Arguments

| Name   | Type   | Required | Description                                                                                           |
| ------ | ------ | -------- | ----------------------------------------------------------------------------------------------------- |
| name   | any    | Yes      | The image to encode. Can be a `BoxImage` object or image name.                                        |
| format | String | No       | The image format for encoding (e.g., "png", "jpg", "webp"). Auto-detected from the source if omitted. |

## Returns

`String` — The base64-encoded string representing the image in the specified or detected format.

## Description

Encodes the specified image as a base64 string. If no `format` is specified, the format is auto-detected from the image's original source (e.g., a JPEG file produces a JPEG base64 string). You can override this by providing an explicit format. Useful for embedding images in HTML, transmitting via APIs, or storing images as text.

## Example

```boxlang
// Encode image as base64 (auto-detect format from source)
base64String = ImageWriteBase64( myImage );

// Encode with explicit format
base64PNG  = ImageWriteBase64( myImage, "png" );
base64JPG  = ImageWriteBase64( myImage, "jpg" );
base64WebP = ImageWriteBase64( myImage, "webp" );

// Member syntax with auto-detect
base64 = myImage.toBase64String();

// Member syntax with explicit format
base64 = myImage.toBase64String( "webp" );
```

## Related BIFs

* ImageReadBase64
* ImageWrite
* ImageGetBlob

## Notes

* The `name` argument can be a `BoxImage` object or the name of an image variable in the current context.
* The `format` argument is optional. When omitted, the format is detected from the image's source file extension.
* Supported formats include "png", "jpg", "webp", "gif", "bmp", "tiff".
* Returns a base64-encoded string suitable for embedding or transmission.
* If encoding fails, an error is thrown.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://boxlang.ortusbooks.com/boxlang-framework/modularity/image-manipulation/reference/built-in-functions/imagewritebase64.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
