Skip to content

Images

Images 在运行 ocd 的主机上对请求体中的图像执行变换,并受尺寸与并发限制。这不是 Cloudflare 托管 Images:不提供图像库、上传签名或 URL 变换。

例如:

  • 缩放请求体中的图像
  • 叠加图像,输出 jpeg / png / webp / avif
  • 使用 info() 读取格式信息
ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    if (!request.body) return new Response("empty", { status: 400 });
    const out = await env.IMAGES
      .input(request.body)
      .transform({ width: 200, fit: "scale-down" })
      .output({ format: "image/webp" });
    return out.response();
  },
} satisfies ExportedHandler<{ IMAGES: ImagesBinding }>;

open-compute.json 中声明。Images 不是 bindings 中的资源 ID,使用顶层字段:

json
{
  "name": "img-app",
  "main": "src/index.ts",
  "images": { "binding": "IMAGES" }
}

语法见 绑定。CLI:oc / oc run / oc types

兼容性

主题Cloudflareopen-compute
Binding APIImages 调用链相同:inputtransform / drawoutputresponse(),以及 info()
产品形态托管 Cloudflare Images对本机请求体中的图像做变换
输入托管图像 ID 或 URLReadableStream 字节
上传 / 签名提供不提供
URL 变换提供不提供
视频提供不提供
AI 放大提供不提供
配置wrangler images"images": { "binding": "IMAGES" }

本节