# image/encode
Encode an image to bytes in the named format, ignoring the image's own
output format. Returns a binary-safe string.
## Syntax
[image/encode $img $fmt ?opts]
## Parameters
- `$img` — an `Image` value
- `$fmt` — `"jpeg"` (or `"jpg"`), `"png"`, or `"webp"` (case-insensitive)
- `$opts` — optional map; `:quality` 1–100 (jpeg/webp) and
`:compression-level` 0–9 (png). Defaults come from the image's current
settings; values are clamped, not errors.
## Examples
set img [image/open "test/fixtures/images/test.png"] [bytes/len [image/encode $img "png"]] # → 115 bytes (fixture) [image/format [image/decode [image/encode $img "jpeg" %{ quality 5 }]]] # → "jpeg" [image/encode $img "bmp"] # → %Err{:message "image/encode: unknown format (jpeg/png/webp)"}
## Returns
A binary-safe string of encoded bytes. `%Err{:message}` for a missing or
unknown format string; `nil` when `$img` is not an `Image` or the encoder
fails.
## See Also
`image/bytes`, `image/write`, `image/base64`, `image/jpeg`, `image/png`, `image/webp`