@tola/image

读取图片元数据、缩放转换图片,以及生成带 srcset 的响应式 <img>。

导入

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

生成的图片统一输出到 _tola/images/ 下,源图与选项相同的多次调用会自动复用同一产物。如需同时发布原图,请使用 [assets]。暂不支持动图输入。

导出成员

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;

缩放或转换图片并发布到 _tola/images/,返回 URL 与像素尺寸。

返回 (url:, width:, height:, original-width:, original-height:, format:, mime:),其中 url 已附带 site.base-path。JPEG 宽高上限为 65,535 px(4:2:0 色度采样),WebP 上限为 16,383 px;SVG 内引用图片须用 data URI。

位置参数

  • source path | str | bytes 必填:文件路径(相对当前 .typ 文件,或 path(...) 值)或图片字节。

命名参数

  • width int | none 默认值: none:目标宽度(正整数像素)。fit-width、scale、fit 与 fill 必填。
  • height int | none 默认值: none:目标高度(正整数像素)。fit-height、scale、fit 与 fill 必填。
  • op str 默认值: "fill":缩放模式:"fill"(默认,裁剪至目标尺寸)、"fit"(等比放入且不放大)、"fit-width"、"fit-height" 或 "scale"(拉伸)。
  • anchor str 默认值: "center":"fill" 裁剪锚点:"center"(默认)、"top"、"bottom"、"left"、"right"、"top-left"、"top-right"、"bottom-left" 或 "bottom-right"。其余模式仅接受 "center"。
  • format str 默认值: "auto":"auto"(默认:无透明通道的有损源图转 JPEG,其余转 PNG)、"jpg"/"jpeg"、"png" 或 "webp"。SVG 必须显式指定位图格式。
  • quality int | none 默认值: none:JPEG 取 1..100(默认 75);WebP 取 0..100(none 为无损)。PNG 忽略。
  • filter str 默认值: "lanczos3":位图重采样滤镜:"lanczos3"(默认)、"nearest"、"triangle"、"catmull-rom" 或 "gaussian"。SVG 忽略。
  • background color | none 默认值: none:填充透明区域的不透明 color。含透明通道图片转为 JPEG 时必填。

将封面图按宽度缩放为 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: "标志")
]

image-metadata

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

读取图片经方向校正后的像素尺寸、格式、MIME 类型及透明/有损标记。

返回 (width:, height:, format:, mime:, has-alpha:, is-lossy:),其中 format 为 "jpg"、"png"、"webp"、"gif"、"bmp" 或 "svg"。

位置参数

  • source path | str | 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;

为位图生成多宽度版本,返回可直接使用的 srcset 字符串与回退 URL。

返回 (url:, width:, height:, srcset:, variants:),url 指向最宽的生成版本(已带 site.base-path)。

位置参数

  • source path | str | bytes 必填:位图文件路径或字节。

命名参数

  • widths any 默认值: auto:auto(默认,取原图宽度)或正整数像素宽度数组(自动排序去重)。
  • format str 默认值: "webp":输出格式:"webp"(默认)、"auto"、"jpg"/"jpeg" 或 "png"。
  • quality int | none 默认值: 75:JPEG 1..100 或 WebP 0..100(默认 75;传 none 输出无损 WebP)。PNG 忽略。
  • background color | none 默认值: none:含透明通道图片转为 JPEG 时必填的不透明底色 color。

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;

生成带像素宽高与自动 srcset/sizes 的响应式 HTML <img> 元素。

用于 HTML 导出;PDF 等分页格式请使用 Typst 内置的 image(...)。

位置参数

  • source path | str | bytes 必填:位图路径或字节。SVG 请直接用 Typst 的 image(...) 或先用 resize-image 转换。

命名参数

  • widths any 默认值: auto:auto(默认,取原图宽度)或正整数像素宽度数组。
  • alt str 默认值: "":替代文本(默认 "",标记装饰图片),始终写入 <img>。
  • sizes str 默认值: "100vw":CSS sizes 提示(默认 "100vw"),仅在去重后至少生成两个宽度时写出。
  • format str 默认值: "webp":输出格式:"webp"(默认)、"auto"、"jpg"/"jpeg" 或 "png"。
  • quality int | none 默认值: 75:JPEG 1..100 或 WebP 0..100(默认 75;none 输出无损 WebP)。PNG 忽略。
  • background color | none 默认值: none:含透明通道图片转为 JPEG 时必填的不透明底色 color。
  • attrs dictionary 默认值: (:):附加字符串 HTML 属性。默认写入 loading: "lazy" 与 decoding: "async",可在此覆盖;由函数托管的属性(src、srcset、sizes、width、height、alt)禁止传入。

渲染优先加载的首屏响应式图片

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

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

站内搜索

可以用上下方向键选择结果。

输入关键词开始搜索。