@tola/image

Inspect image metadata, resize/convert images, and generate responsive <img> elements with srcset.

Import

typst
#import "@tola/image:0.0.0": *

Generated images are written under _tola/images/ and deduplicated automatically when identical calls share the same source and options. Use [assets] if you also want to publish the untouched original image. Animated images are rejected.

Exports

resize-image

typc
let resize-image(
  source: path | str | bytes,
  anchor: str = "center",
  background: color | none = none,
  filter: str = "lanczos3",
  format: str = "auto",
  height: int | none = none,
  op: str = "fill",
  quality: int | none = none,
  width: int | none = none,
) = dictionary;

Resize or convert an image, publish it under _tola/images/, and return its URL and dimensions.

Returns (url:, width:, height:, original-width:, original-height:, format:, mime:) with site.base-path already applied to url. JPEG dimensions are limited to 65,535 px (4:2:0 chroma subsampling); WebP is limited to 16,383 px. SVG external images must use data URIs.

Positional parameters

  • source path | str | bytes required: File path (relative to the calling .typ file, or a path(...) value) or image bytes.

Named parameters

  • width int | none default: none: Positive target width in pixels. Required by fit-width, scale, fit, and fill.
  • height int | none default: none: Positive target height in pixels. Required by fit-height, scale, fit, and fill.
  • op str default: "fill": Resize operation: "fill" (default, crop to exact dimensions), "fit" (contain without upscaling), "fit-width", "fit-height", or "scale" (stretch).
  • anchor str default: "center": Crop anchor for "fill": "center" (default), "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", or "bottom-right". Other modes accept only "center".
  • format str default: "auto": "auto" (default: JPEG for opaque lossy sources, PNG otherwise), "jpg" / "jpeg", "png", or "webp". SVG inputs require an explicit raster format.
  • quality int | none default: none: JPEG 1..100 (default 75); WebP 0..100 (none for lossless). Ignored for PNG.
  • filter str default: "lanczos3": Raster resampling filter: "lanczos3" (default), "nearest", "triangle", "catmull-rom", or "gaussian". Ignored for SVG.
  • background color | none default: none: Opaque background color composited under transparent pixels. Required when converting images with alpha to JPEG.

Resize a cover image to WebP

typst
#import "@tola/image:0.0.0": resize-image

#let thumb = resize-image("assets/logo.png", width: 200, op: "fit-width", format: "webp")
#document("index.html")[
  #html.img(src: thumb.url, width: str(thumb.width), height: str(thumb.height), alt: "Logo")
]

image-metadata

typc
let image-metadata(
  source: path | str | bytes,
) = dictionary;

Read an image’s orientation-adjusted pixel dimensions, format, MIME type, alpha, and lossiness.

Returns (width:, height:, format:, mime:, has-alpha:, is-lossy:) where format is one of "jpg", "png", "webp", "gif", "bmp", or "svg".

Positional parameters

  • source path | str | bytes required: File path or image bytes.

image-srcset

typc
let image-srcset(
  source: path | str | bytes,
  background: color | none = none,
  format: str = "webp",
  quality: int | none = 75,
  widths: any = auto,
) = dictionary;

Generate multiple width variants of a raster image and return a ready-to-use srcset string plus fallback URL.

Returns (url:, width:, height:, srcset:, variants:) where url points to the widest generated variant (with site.base-path).

Positional parameters

  • source path | str | bytes required: Raster image path or bytes.

Named parameters

  • widths any default: auto: auto (default, source width) or array of positive pixel widths (sorted and deduplicated).
  • format str default: "webp": Output format: "webp" (default), "auto", "jpg" / "jpeg", or "png".
  • quality int | none default: 75: JPEG 1..100 or WebP 0..100 (default 75; pass none for lossless WebP). Ignored for PNG.
  • background color | none default: none: Opaque background color required when converting transparent images to JPEG.

img

typc
let img(
  source: path | str | bytes,
  alt: str = "",
  attrs: dictionary = (:),
  background: color | none = none,
  format: str = "webp",
  quality: int | none = 75,
  sizes: str = "100vw",
  widths: any = auto,
) = content;

Emit a responsive HTML <img> element with width, height, and automatic srcset/sizes.

Intended for HTML export; use Typst’s built-in image(...) for PDF or paged targets.

Positional parameters

  • source path | str | bytes required: Raster image path or bytes. For SVG, use Typst’s image(...) or resize-image first.

Named parameters

  • widths any default: auto: auto (default, source width) or array of positive pixel widths.
  • alt str default: "": Alternative text (default "", marking decorative images). Always written to the <img> tag.
  • sizes str default: "100vw": CSS sizes hint (default "100vw"). Emitted when at least two distinct widths are generated.
  • format str default: "webp": Output format: "webp" (default), "auto", "jpg" / "jpeg", or "png".
  • quality int | none default: 75: JPEG 1..100 or WebP 0..100 (default 75; none for lossless WebP). Ignored for PNG.
  • background color | none default: none: Opaque background color required when converting transparent images to JPEG.
  • attrs dictionary default: (:): Extra string-valued HTML attributes. Defaults loading: "lazy" and decoding: "async" can be overridden here; managed attributes (src, srcset, sizes, width, height, alt) are rejected.

Render a responsive hero image with eager loading

typst
#import "@tola/image:0.0.0": img

#document("index.html")[
  #img(
    "assets/logo.png",
    widths: (480, 960),
    alt: "Hero banner",
    sizes: "(max-width: 760px) 100vw, 720px",
    attrs: (loading: "eager", fetchpriority: "high"),
  )
]

Search this site

Use the arrow keys to move through results.

Type to search.