@tola/image
Inspect image metadata, resize/convert images, and generate responsive <img> elements with srcset.
Import
#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
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
sourcepath | str | bytesrequired: File path (relative to the calling.typfile, or apath(...)value) or image bytes.
Named parameters
widthint | nonedefault:none: Positive target width in pixels. Required byfit-width,scale,fit, andfill.heightint | nonedefault:none: Positive target height in pixels. Required byfit-height,scale,fit, andfill.opstrdefault:"fill": Resize operation:"fill"(default, crop to exact dimensions),"fit"(contain without upscaling),"fit-width","fit-height", or"scale"(stretch).anchorstrdefault:"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".formatstrdefault:"auto":"auto"(default: JPEG for opaque lossy sources, PNG otherwise),"jpg"/"jpeg","png", or"webp". SVG inputs require an explicit raster format.qualityint | nonedefault:none: JPEG1..100(default75); WebP0..100(nonefor lossless). Ignored for PNG.filterstrdefault:"lanczos3": Raster resampling filter:"lanczos3"(default),"nearest","triangle","catmull-rom", or"gaussian". Ignored for SVG.backgroundcolor | nonedefault:none: Opaque backgroundcolorcomposited under transparent pixels. Required when converting images with alpha to JPEG.
Resize a cover image to WebP
#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
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
sourcepath | str | bytesrequired: File path or image bytes.
image-srcset
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
sourcepath | str | bytesrequired: Raster image path or bytes.
Named parameters
widthsanydefault:auto:auto(default, source width) or array of positive pixel widths (sorted and deduplicated).formatstrdefault:"webp": Output format:"webp"(default),"auto","jpg"/"jpeg", or"png".qualityint | nonedefault:75: JPEG1..100or WebP0..100(default75; passnonefor lossless WebP). Ignored for PNG.backgroundcolor | nonedefault:none: Opaque backgroundcolorrequired when converting transparent images to JPEG.
img
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
sourcepath | str | bytesrequired: Raster image path or bytes. For SVG, use Typst’simage(...)orresize-imagefirst.
Named parameters
widthsanydefault:auto:auto(default, source width) or array of positive pixel widths.altstrdefault:"": Alternative text (default"", marking decorative images). Always written to the<img>tag.sizesstrdefault:"100vw": CSSsizeshint (default"100vw"). Emitted when at least two distinct widths are generated.formatstrdefault:"webp": Output format:"webp"(default),"auto","jpg"/"jpeg", or"png".qualityint | nonedefault:75: JPEG1..100or WebP0..100(default75;nonefor lossless WebP). Ignored for PNG.backgroundcolor | nonedefault:none: Opaque backgroundcolorrequired when converting transparent images to JPEG.attrsdictionarydefault:(:): Extra string-valued HTML attributes. Defaultsloading: "lazy"anddecoding: "async"can be overridden here; managed attributes (src,srcset,sizes,width,height,alt) are rejected.
Render a responsive hero image with eager loading
#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"),
)
]