# GPT Image 2 / 2.5 / 2.6 图片生成与编辑

使用 GPT Image 系列模型异步生成或编辑图片。创建任务后保存 `task_id` 并查询状态；成功后从 `images[].url` 获取结果。

支持模型：`gpt-image-2`、`gpt-image-2.5-flare`、`gpt-image-2.5-sunburst`、`gpt-image-2.6-flare`、`gpt-image-2.6-sunburst`。五个模型的接口、参数和任务流程相同；唯一区别是 GIF 动图输出只有 `gpt-image-2.6-flare`、`gpt-image-2.6-sunburst` 支持。

## 快速选择

Base URL：`https://vip.brioi.com`  
鉴权：`Authorization: Bearer YOUR_API_KEY`

**图片生成**

- 创建：`POST /v1/images/async/generations`
- 查询：`GET /v1/images/async/generations/{task_id}`
- 请求：JSON，只传提示词及生成参数。

**图片 URL 编辑（推荐）**

- 创建：`POST /v1/images/async/edits`
- 查询：`GET /v1/images/async/edits/{task_id}`
- 请求：JSON，使用 `images` 传入公网图片 URL。

**本地图片编辑**

- 创建：`POST /v1/images/async/edits`
- 查询：`GET /v1/images/async/edits/{task_id}`
- 请求：`multipart/form-data`，使用 `image[]` 上传文件。

兼容说明：`POST /v1/images/generations` 和 `POST /v1/images/edits` 仅用于存量调用，新接入请使用异步接口。

## 图片生成

### 生成参数

| 参数 | 必填 | 说明 |
|---|---:|---|
| `model` | 是 | string；填写支持模型之一。 |
| `prompt` | 是 | string；图片生成提示词。 |
| `size` | 否 | string，默认 `auto`；自定义尺寸见“参数限制与错误”。 |
| `quality` | 否 | string，默认 `auto`；支持 `auto`、`high`、`medium`、`low`。 |
| `n` | 否 | integer，默认 `1`；推荐填 `1`（多数规格单次生成 1 张，需要多张图片时建议并发发起多个请求）。 |
| `output_format` | 否 | string；支持 `png`、`jpeg`、`webp`；`gpt-image-2.6-flare`、`gpt-image-2.6-sunburst` 另支持 `gif`，见“GIF 动图输出”。 |
| `background` | 否 | string，默认 `auto`；支持 `auto`、`opaque`，不支持透明背景。 |
| `output_compression` | 否 | integer；JPEG 或 WebP 压缩质量，范围 0 至 100。 |

### 生成请求示例

```bash
curl -X POST "https://vip.brioi.com/v1/images/async/generations" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "一只橙色小猫坐在白色窗台上，柔和自然光，简洁摄影风格",
    "size": "1024x1024",
    "quality": "auto",
    "n": 1
  }'
```

## 图片编辑

### 编辑参数

`image[]` 和 `images` 二选一：本地文件使用 `image[]`，公网图片 URL 使用 `images`。

**核心输入**

| 参数 | 必填 | 说明 |
|---|---:|---|
| `model` | 是 | string；填写支持模型之一。 |
| `prompt` | 是 | string；说明需要保留和修改的内容。 |
| `image[]` | 条件必填 | file；本地文件请求使用，至少上传 1 张，可重复传入。 |
| `images` | 条件必填 | array；JSON 请求使用，数组项可为 URL 字符串或 `{ "image_url": "..." }`。 |

**编辑控制**

| 参数 | 必填 | 说明 |
|---|---:|---|
| `mask` | 否 | file 或 string；PNG 遮罩图。JSON 请求可传公网 URL，尺寸应与第一张参考图一致。 |
| `input_fidelity` | 否 | string；支持 `low`、`high`。 |
| `background` | 否 | string，默认 `auto`；支持 `auto`、`opaque`，不支持透明背景。 |
| `size` | 否 | string，默认 `auto`；自定义尺寸见“参数限制与错误”。 |
| `quality` | 否 | string，默认 `auto`；支持 `auto`、`high`、`medium`、`low`。 |
| `n` | 否 | integer，默认 `1`；建议使用 `1`。 |

**输出控制**

| 参数 | 必填 | 说明 |
|---|---:|---|
| `output_format` | 否 | string；支持 `png`、`jpeg`、`webp`；`gpt-image-2.6-flare`、`gpt-image-2.6-sunburst` 另支持 `gif`，见“GIF 动图输出”。 |
| `output_compression` | 否 | integer；JPEG 或 WebP 压缩质量，范围 0 至 100。 |

### URL 编辑示例（推荐）

```bash
curl -X POST "https://vip.brioi.com/v1/images/async/edits" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "保留人物和服装，把场景改成雨夜街道",
    "images": [
      "https://example.com/reference-1.png",
      "https://example.com/reference-2.jpg"
    ],
    "size": "1024x1024",
    "quality": "auto",
    "n": 1
  }'
```

### 本地文件示例

```bash
curl -X POST "https://vip.brioi.com/v1/images/async/edits" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -F "model=gpt-image-2" \
  -F "prompt=保留商品形状和材质，把背景替换为干净的白色摄影棚" \
  -F "image[]=@/path/to/reference-1.png" \
  -F "image[]=@/path/to/reference-2.png" \
  -F "size=1024x1024" \
  -F "quality=auto"
```

## GIF 动图输出

仅 `gpt-image-2.6-flare` 和 `gpt-image-2.6-sunburst` 支持。生成和编辑接口都可以使用：传 `output_format: "gif"`，在提示词里描述主体、动作和循环方式；需要用静态图引导动画时，把参考图传给编辑接口。

| 参数 | 必填 | 说明 |
|---|---:|---|
| `output_format` | 是 | string；填 `gif`。 |
| `frame_count` | 否 | integer，默认 `9`；帧数，范围 2 至 16。 |
| `frame_duration_ms` | 否 | integer，默认 `200`；每帧时长（毫秒），范围 20 至 2000，按 10 毫秒取整。 |
| `size` | 否 | string，默认 `auto`；指每一帧的尺寸。 |

- 所有帧合在一起计算像素，帧数越多、每帧越大越容易超限；超出时返回参数错误。遇到这种错误，减少 `frame_count`、调小 `size`，或用 `size: "auto"` 让模型自动选择帧尺寸。
- `frame_count`、`frame_duration_ms` 只用于 GIF 输出；GIF 不支持 `output_compression`。
- 成功后 `images[].mime_type` 为 `image/gif`，从 `images[].url` 下载完整动图。

```bash
curl -X POST "https://vip.brioi.com/v1/images/async/generations" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2.6-flare",
    "prompt": "一只橙色小猫在窗台上慢慢眨眼，尾巴轻轻摆动，镜头不动，首尾自然循环",
    "size": "auto",
    "quality": "auto",
    "n": 1,
    "output_format": "gif",
    "frame_count": 6,
    "frame_duration_ms": 200
  }'
```

## 任务状态与结果

**创建响应**

```json
{"status":"SUBMITTED","task_id":"task_xxxxxxxxxxxxxxxxxxxxxxxx",
 "progress":"10%","images":[]}
```

保存 `task_id`。创建成功只表示任务已进入处理流程，不代表图片已经生成完成。

**任务状态**

建议每 3 至 5 秒调用对应查询接口，直到进入终态：

| 状态 | 说明 |
|---|---|
| `SUBMITTED` | 已提交，继续查询。 |
| `IN_PROGRESS` | 正在处理，继续查询。 |
| `SUCCESS` | 已完成，从 `images[].url` 获取结果。 |
| `FAILURE` | 已失败，从 `fail_reason` 获取原因。 |

**成功结果**

```json
{
  "task_id": "task_xxxxxxxxxxxxxxxxxxxxxxxx",
  "status": "SUCCESS",
  "progress": "100%",
  "images": [
    {
      "index": 0,
      "url": "https://example.com/result.png",
      "mime_type": "image/png"
    }
  ]
}
```

部分存储可能返回 `application/octet-stream`。结果 URL 带有效期，请及时下载或转存。查询响应还可能包含 `usage` 等辅助字段，客户端不要依赖。

失败响应示例：

```json
{
  "task_id": "task_xxxxxxxxxxxxxxxxxxxxxxxx",
  "status": "FAILURE",
  "progress": "100%",
  "fail_reason": "{\"error\":{\"code\":\"invalid_input\",\"message\":\"参数不正确或素材无法访问\"}}"
}
```

`fail_reason` 是排查用的原始错误文本，可能包含 JSON 片段，不要按固定格式解析。

## 参数限制与错误

- 图片编辑每次最多 16 张参考图。
- JSON 中的参考图和遮罩图必须是公网可访问的 HTTPS 图片直链。
- 不支持 `background: "transparent"`。
- 自定义 `size` 必须同时满足：
  - 宽和高均为 16 的倍数；
  - 最大边不超过 3840 像素；
  - 长边与短边之比不超过 3:1；
  - 总像素数不少于 655,360，且不超过 8,294,400。
- `size`、`quality`、`output_format` 或素材格式不符合要求时，会返回参数错误。

| 状态码 | 说明 |
|---|---|
| `400` | 请求参数错误。 |
| `401` | API Key 缺失或无效。 |
| `403` | 余额或额度不足，或没有该模型的权限。 |
| `404` | 查询的任务不存在。 |
| `429` | 请求过于频繁。 |
| `503` | 服务暂时不可用 |
| `500`、`502` | 服务暂时异常。 |

