cloudflare-images
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloudflare Images
Cloudflare Images
End-to-end image storage, transformation, and delivery solution on Cloudflare's global network.
基于Cloudflare全球网络的端到端图片存储、转换与交付解决方案。
Quick Navigation
快速导航
- Upload methods →
references/upload.md - Transformations →
references/transformations.md - Variants →
references/variants.md - Workers binding →
references/binding.md - Polish →
references/polish.md - Signed URLs →
references/security.md - Pricing →
references/pricing.md
- 上传方式 →
references/upload.md - 图片转换 →
references/transformations.md - 变体 →
references/variants.md - Workers绑定 →
references/binding.md - Polish →
references/polish.md - 签名URL →
references/security.md - 定价 →
references/pricing.md
When to Use
适用场景
- Storing and delivering optimized images at scale
- Transforming remote images on-the-fly
- Resizing, cropping, converting image formats
- Adding watermarks to images
- Serving responsive images with
srcset - Protecting images with signed URLs
- Optimizing images from R2 storage
- 大规模存储并交付经过优化的图片
- 实时转换远程图片
- 调整图片大小、裁剪、转换图片格式
- 为图片添加水印
- 使用提供响应式图片
srcset - 用签名URL保护图片
- 优化来自R2存储的图片
Two Usage Modes
两种使用模式
| Mode | Description | Billing |
|---|---|---|
| Storage in Images | Upload images to Cloudflare, serve via variants | Images Stored + Images Delivered |
| Transform remote | Optimize images from any origin (R2, S3, etc.) | Images Transformed (unique/30d) |
| 模式 | 描述 | 计费方式 |
|---|---|---|
| 存储至Images | 将图片上传至Cloudflare,通过变体交付 | 存储图片量 + 交付图片量 |
| 转换远程图片 | 优化来自任意源(R2、S3等)的图片 | 图片转换量(30天内唯一请求) |
Quick Start
快速开始
Upload an Image (API)
通过API上传图片
bash
curl --request POST \
--url https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1 \
--header 'Authorization: Bearer <API_TOKEN>' \
--header 'Content-Type: multipart/form-data' \
--form file=@./image.jpgbash
curl --request POST \
--url https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1 \
--header 'Authorization: Bearer <API_TOKEN>' \
--header 'Content-Type: multipart/form-data' \
--form file=@./image.jpgTransform via URL
通过URL转换图片
html
<img src="/cdn-cgi/image/width=400,quality=80,format=auto/uploads/hero.jpg" />html
<img src="/cdn-cgi/image/width=400,quality=80,format=auto/uploads/hero.jpg" />Transform via Workers
通过Workers转换图片
js
fetch(imageURL, {
cf: {
image: {
width: 800,
height: 600,
fit: "cover",
format: "auto",
},
},
});js
fetch(imageURL, {
cf: {
image: {
width: 800,
height: 600,
fit: "cover",
format: "auto",
},
},
});URL Format
URL格式
https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>- — your Cloudflare domain
<ZONE> - — fixed prefix for image transformations
/cdn-cgi/image/ - — comma-separated:
<OPTIONS>width=400,quality=80,format=auto - — absolute path or full URL
<SOURCE-IMAGE>
https://<ZONE>/cdn-cgi/image/<OPTIONS>/<SOURCE-IMAGE>- — 你的Cloudflare域名
<ZONE> - — 图片转换的固定前缀
/cdn-cgi/image/ - — 逗号分隔的参数:
<OPTIONS>width=400,quality=80,format=auto - — 绝对路径或完整URL
<SOURCE-IMAGE>
Stored Images Delivery
已存储图片的交付格式
https://imagedelivery.net/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>https://imagedelivery.net/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>Transformation Options
转换选项
| Option | Description | Example |
|---|---|---|
| Max width in pixels | |
| Max height in pixels | |
| Resize mode | |
| Output format | |
| JPEG/WebP/AVIF quality 1-100 | |
| Crop focus point | |
| Blur radius 1-250 | |
| Sharpening 0-10 | |
| Rotation degrees | |
| Remove pixels from edges | |
| 选项 | 描述 | 示例 |
|---|---|---|
| 最大宽度(像素) | |
| 最大高度(像素) | |
| 缩放模式 | |
| 输出格式 | |
| JPEG/WebP/AVIF质量(1-100) | |
| 裁剪焦点 | |
| 模糊半径(1-250) | |
| 锐化程度(0-10) | |
| 旋转角度(度) | |
| 移除边缘像素 | |
Fit Modes
缩放模式
| Mode | Behavior |
|---|---|
| Shrink only, never enlarge |
| Fit within dimensions, preserve aspect ratio |
| Fill dimensions, crop if needed |
| Like cover but never enlarges |
| Fit within, add background color |
| 模式 | 行为 |
|---|---|
| 仅缩小,绝不放大 |
| 适配尺寸,保留宽高比 |
| 填充尺寸,必要时裁剪 |
| 类似cover,但绝不放大 |
| 适配尺寸,添加背景色 |
Format Auto
自动格式
format=autoformat=autoSupported Formats
支持的格式
Input
输入格式
- JPEG, PNG, GIF (animated), WebP (animated), SVG, HEIC
- JPEG、PNG、GIF(动图)、WebP(动图)、SVG、HEIC
Output
输出格式
- JPEG, PNG, GIF, WebP, AVIF, SVG (passthrough)
Note: HEIC must be served as AVIF/WebP/JPEG/PNG. SVG is not resized (inherently scalable).
- JPEG、PNG、GIF、WebP、AVIF、SVG(直接透传)
注意: HEIC必须转换为AVIF/WebP/JPEG/PNG格式后交付。SVG不会被调整大小(本身是矢量图,可缩放)。
Limits
限制条件
| Constraint | Limit |
|---|---|
| Max image dimension | 12,000 pixels |
| Max image area | 100 megapixels |
| Max file size (transformations) | 70 MB |
| Max file size (storage) | 10 MB |
| GIF/WebP animation | 50 megapixels total |
| AVIF output hard limit | 1,200 px (1,600 explicit) |
| Variants per account | 100 |
| 约束条件 | 限制值 |
|---|---|
| 最大图片尺寸 | 12,000像素 |
| 最大图片面积 | 1亿像素 |
| 转换时最大文件大小 | 70 MB |
| 存储时最大文件大小 | 10 MB |
| GIF/WebP动图 | 总像素不超过5000万 |
| AVIF输出硬限制 | 1200像素(显式设置为1600) |
| 每个账户的变体数量 | 100 |
Workers Integration
Workers集成
Images Binding Setup
Images绑定设置
toml
undefinedtoml
undefinedwrangler.toml
wrangler.toml
[images]
binding = "IMAGES"
undefined[images]
binding = "IMAGES"
undefinedTransform with Binding
通过绑定转换图片
ts
const response = (await env.IMAGES.input(stream).transform({ width: 800 }).transform({ blur: 20 }).output({ format: "image/avif" })).response();ts
const response = (await env.IMAGES.input(stream).transform({ width: 800 }).transform({ blur: 20 }).output({ format: "image/avif" })).response();Draw Watermark
添加水印
ts
const watermark = await fetch("https://example.com/watermark.png");
const image = await fetch("https://example.com/photo.jpg");
const response = (
await env.IMAGES.input(image.body)
.draw(env.IMAGES.input(watermark.body).transform({ width: 100 }), { bottom: 10, right: 10, opacity: 0.75 })
.output({ format: "image/avif" })
).response();ts
const watermark = await fetch("https://example.com/watermark.png");
const image = await fetch("https://example.com/photo.jpg");
const response = (
await env.IMAGES.input(image.body)
.draw(env.IMAGES.input(watermark.body).transform({ width: 100 }), { bottom: 10, right: 10, opacity: 0.75 })
.output({ format: "image/avif" })
).response();Get Image Info
获取图片信息
ts
const info = await env.IMAGES.info(stream);
// { format, fileSize, width, height }ts
const info = await env.IMAGES.info(stream);
// { format, fileSize, width, height }Recipes
实践方案
Responsive Images with srcset
使用srcset实现响应式图片
html
<img srcset="/cdn-cgi/image/width=320/photo.jpg 320w, /cdn-cgi/image/width=640/photo.jpg 640w, /cdn-cgi/image/width=1280/photo.jpg 1280w" sizes="(max-width: 640px) 100vw, 640px" src="/cdn-cgi/image/width=640/photo.jpg" alt="Responsive image" />html
<img srcset="/cdn-cgi/image/width=320/photo.jpg 320w, /cdn-cgi/image/width=640/photo.jpg 640w, /cdn-cgi/image/width=1280/photo.jpg 1280w" sizes="(max-width: 640px) 100vw, 640px" src="/cdn-cgi/image/width=640/photo.jpg" alt="Responsive image" />Face-Aware Cropping
人脸识别裁剪
js
fetch(imageURL, {
cf: {
image: {
width: 200,
height: 200,
fit: "cover",
gravity: "face",
zoom: 0.5, // 0 = more background, 1 = tight crop
},
},
});js
fetch(imageURL, {
cf: {
image: {
width: 200,
height: 200,
fit: "cover",
gravity: "face",
zoom: 0.5, // 0 = 保留更多背景,1 = 紧密裁剪
},
},
});Direct Creator Upload
Direct Creator Upload
bash
undefinedbash
undefinedGet one-time upload URL
获取一次性上传URL
curl --request POST
https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v2/direct_upload
--header "Authorization: Bearer <API_TOKEN>"
https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v2/direct_upload
--header "Authorization: Bearer <API_TOKEN>"
curl --request POST
https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v2/direct_upload
--header "Authorization: Bearer <API_TOKEN>"
https://api.cloudflare.com/client/v4/accounts/{account_id}/images/v2/direct_upload
--header "Authorization: Bearer <API_TOKEN>"
Response: { "uploadURL": "https://upload.imagedelivery.net/..." }
响应:{ "uploadURL": "https://upload.imagedelivery.net/..." }
undefinedundefinedUpload from Worker
通过Worker上传图片
ts
const image = await fetch("https://example.com/image.png");
const bytes = await image.bytes();
const formData = new FormData();
formData.append("file", new File([bytes], "image.png"));
await fetch(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`, {
method: "POST",
headers: { Authorization: `Bearer ${TOKEN}` },
body: formData,
});ts
const image = await fetch("https://example.com/image.png");
const bytes = await image.bytes();
const formData = new FormData();
formData.append("file", new File([bytes], "image.png"));
await fetch(`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`, {
method: "POST",
headers: { Authorization: `Bearer ${TOKEN}` },
body: formData,
});Critical Prohibitions
重要禁忌
- NEVER set up Image Resizing Worker for entire zone () — blocks non-image requests
/* - NEVER activate Polish and image transformations simultaneously — redundant compression
- NEVER use flexible variants with signed URL tokens — incompatible
- NEVER use custom ID paths with — not supported
requireSignedURLs=true - NEVER expect SVG resizing — SVGs are passed through as-is
- NEVER include resizing options in — fragments cache
cacheKey
- 切勿为整个域名()设置图片重定向Worker —— 会阻止非图片请求
/* - 切勿同时启用Polish和图片转换 —— 重复压缩
- 切勿将灵活变体与签名URL令牌配合使用 —— 不兼容
- 切勿在时使用自定义ID路径 —— 不支持
requireSignedURLs=true - 切勿期望SVG能被调整大小 —— SVG会直接透传
- 切勿在中包含重定向选项 —— 会碎片化缓存
cacheKey
Troubleshooting
故障排查
No Cf-Resized
Header
Cf-Resized无Cf-Resized
响应头
Cf-Resized- Transformations not enabled on zone
- Another Worker intercepting request
- Using dashboard preview (doesn't simulate transforms)
- 域名未启用图片转换
- 其他Worker拦截了请求
- 使用了控制台预览(不会模拟转换效果)
Common Error Codes
常见错误码
| Code | Meaning |
|---|---|
| 9401 | Invalid transformation options |
| 9402 | Image too large |
| 9403 | Request loop detected |
| 9422 | Free tier limit exceeded (5,000/month) |
| 9520 | Unsupported format |
| 错误码 | 含义 |
|---|---|
| 9401 | 无效的转换选项 |
| 9402 | 图片过大 |
| 9403 | 检测到请求循环 |
| 9422 | 免费版限额已超出(每月5000次) |
| 9520 | 不支持的格式 |
Related Skills
相关技能
- - For serverless compute
cloudflare-workers - - For full-stack apps
cloudflare-pages - - R2 object storage
cloudflare-r2
- - 用于无服务器计算
cloudflare-workers - - 用于全栈应用
cloudflare-pages - - R2对象存储
cloudflare-r2