---
url: https://void.cloud/guide/image-generation.md
description: Generate PNGs from HTML or JSX in a route handler.
---

# Image Generation

Return an `ImageResponse` from a route to draw a PNG with HTML and CSS.

## Draw a badge {#usage}

Read the badge label from the URL and put it in an `html` template:

```ts
// routes/badge.png.ts
import { defineHandler } from 'void';
import { html } from 'void/html';
import { ImageResponse } from 'void/image';

export const GET = defineHandler((c) => {
  const label = c.req.query('label') ?? 'Built with Void';
  return new ImageResponse(
    html`<div style="padding: 12px; background: #0f172a; color: white; font-size: 28px">
      ${label}
    </div>`,
    { width: 320, height: 64 },
  );
});
```

Open `/badge.png?label=Preview` to see a 320 × 64 badge that reads “Preview”. The [`html` tag](../reference/api/html.md) escapes the label, so text like `<Preview>` appears as written. For a social preview, use [`OgImageResponse`](./og-images.md) to get the standard 1200 × 630 size.

## Rendering with JSX {#jsx-types}

To draw a favicon with Hono JSX, add `/** @jsxImportSource void/jsx */` at the top of the `.tsx` route. This works alongside a React, Vue, Svelte, or Solid app without changing how your pages render.

```tsx
/** @jsxImportSource void/jsx */
// routes/favicon.png.tsx
import { ImageResponse } from 'void/image';

export const GET = () =>
  new ImageResponse(
    <div style={{ background: '#0f172a', color: 'white', fontSize: 48, textAlign: 'center' }}>
      V
    </div>,
    { width: 64, height: 64 },
  );
```

To use Hono JSX for every `.tsx` file, set `jsxImportSource` in your [TypeScript and Vite config](./server-routing.md#jsx-handlers). Image routes accept Hono elements and `html` templates, including async templates. Use `ImageInput` to type shared card content.

## Where images render {#setup}

Void uses local Chromium in development, preview, and [prerendering](./edge/prerendering.md). Importing `void/image` adds a Browser Run binding named `BROWSER` for deployment.

* **Your Cloudflare account:** `void deploy --platform cloudflare` uses your account’s [Browser Run allowance](https://developers.cloudflare.com/browser-run/limits/). [Quick Actions](https://developers.cloudflare.com/browser-run/quick-actions/) require a Worker compatibility date of `2026-03-24` or later. If your app pins an older date, update `worker.compatibility_date` in `void.config.ts`.
* **A Void platform:** each render counts toward your account’s [monthly browser time](./platform/administration/plans.md) and your app’s request rate. The binding supports Quick Actions on the default browser, except `json`. For Browser Sessions, deploy to your own Cloudflare account. If the platform predates Browser Run support, ask its administrator to upgrade before deploying.

To test against your Cloudflare account during development, run `void connect --platform cloudflare` and set `remote: true`:

```ts
// void.config.ts
import { defineConfig } from 'void/config';

export default defineConfig({
  cloudflare: { browser: { binding: 'BROWSER', remote: true } },
});
```

## Await rendering

Use `renderImage()` when you need the PNG bytes, for example to save the badge to storage or return a fallback if rendering fails:

```ts
import { html } from 'void/html';
import { renderImage } from 'void/image';

export function renderBadge(label: string) {
  return renderImage(html`<strong>${label}</strong>`, { width: 320, height: 64 });
}
```

Call `await renderBadge(label)` in your handler to catch a rendering error before sending headers. An `ImageResponse` starts rendering when its body is read, after the handler has returned. In a Void handler or Pages action, an uncaught `UsageLimit` from `void/limits` returns HTTP 429. A platform rate limit includes `retryAt`. A monthly quota resets at the next billing period.

## Options

For shared options, import `RenderImageOptions` or `ImageResponseOptions` from `void/image`.

| Option                            | Behavior                                                                             |
| --------------------------------- | ------------------------------------------------------------------------------------ |
| `width`, `height`                 | Required image dimensions in pixels. Use positive integers.                          |
| `font`, `fonts`                   | Select a font family or fallback stack and supply custom faces. See [Fonts](#fonts). |
| `lang`                            | Set the document language, for example `'ja'`.                                       |
| `debug`                           | Set to `true` to outline each element.                                               |
| `status`, `statusText`, `headers` | HTTP options for `ImageResponse`. The content type defaults to `image/png`.          |

### CSS and images

Use inline CSS or a `<style>` block. The browser does not run JavaScript. Use public HTTPS URLs or data URLs for images. If a request supplies an image URL, check that it points to a source you trust.

## Fonts

Text uses Roboto by default. Choose a [preinstalled family](https://developers.cloudflare.com/browser-run/reference/supported-fonts/) or supply a font file as bytes (`ArrayBuffer` or `Uint8Array`) or an HTTPS URL. Set `font` to the name you gave the face:

```ts
import { html } from 'void/html';
import { ImageResponse } from 'void/image';

export function brandBadge(label: string) {
  return new ImageResponse(html`<strong>${label}</strong>`, {
    width: 320,
    height: 64,
    fonts: [{ name: 'Brand', data: { url: 'https://example.com/fonts/brand.woff2' } }],
    font: ['Brand', 'Roboto', 'sans-serif'],
  });
}
```

If you use Google Fonts, use the font file URL. Set `weight` (1–1000) for a single weight or omit it for a variable face. Use `style: 'italic'` for an italic face. Emoji use the browser’s system fonts.

## Cache generated images

`ImageResponse` leaves caching to your route. To reuse the badge at the edge for a day and keep a separate image for each label, add the route and its query parameter to your [revalidation settings](./edge/revalidation.md):

```ts
// void.config.ts
import { defineConfig } from 'void/config';

export default defineConfig({
  routing: {
    revalidate: { '/badge.png': 86400 },
    revalidateQueryAllowlist: { '/badge.png': ['label'] },
  },
});
```

Void skips paths under `/api`. For an image that changes with a query parameter, list that parameter as above or put the value in the path.

## Third-party renderers {#third-party-renderer}

Pass a `renderer` to use another backend, such as [void-image-takumi](https://github.com/yangshun/void-image-takumi) for browser-free rendering. If every image in your app uses a third-party renderer, set [`inference.bindings.browser`](../reference/config.md#inference-bindings) to `false` to leave out the Browser Run binding.
