curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "星空下的古老城堡,電影感光影,超寫實風格",
"size": "16:9",
"resolution": "2k",
"quality": "high",
"n": 1
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "雪山日出全景,極致細節",
"size": "16:9",
"resolution": "4k",
"quality": "high",
"output_format": "jpeg",
"output_compression": 90
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "將兩張參考圖融合成一張插畫海報,保留主體輪廓",
"size": "1:1",
"quality": "high",
"image_urls": [
"https://example.com/input-a.png",
"https://example.com/input-b.png"
]
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "把背景換成沙漠日落",
"size": "1:1",
"quality": "medium",
"image_urls": ["https://example.com/photo.png"],
"mask_url": "https://example.com/mask.png"
}'
const response = await fetch('https://toapis.com/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-ToAPIs-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-image-2-official',
prompt: '星空下的古老城堡,電影感光影,超寫實風格',
size: '16:9',
resolution: '2k',
quality: 'high',
n: 1
})
});
const task = await response.json();
console.log(task.id, task.status);
{
"id": "tsk_img_01KPTXXXXXXXXXXXXXXX",
"object": "generation.task",
"model": "gpt-image-2-official",
"status": "queued",
"progress": 0,
"created_at": 1703884800,
"metadata": {}
}
GPT-Image-2
GPT-Image-2 官方渠道 圖像生成
使用 OpenAI 官方 gpt-image-2-official 模型生成圖像,支持文生圖 / 圖生圖 / 局部重繪(mask)三合一,1K / 2K / 4K 分辨率檔位
POST
/
v1
/
images
/
generations
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "星空下的古老城堡,電影感光影,超寫實風格",
"size": "16:9",
"resolution": "2k",
"quality": "high",
"n": 1
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "雪山日出全景,極致細節",
"size": "16:9",
"resolution": "4k",
"quality": "high",
"output_format": "jpeg",
"output_compression": 90
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "將兩張參考圖融合成一張插畫海報,保留主體輪廓",
"size": "1:1",
"quality": "high",
"image_urls": [
"https://example.com/input-a.png",
"https://example.com/input-b.png"
]
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "把背景換成沙漠日落",
"size": "1:1",
"quality": "medium",
"image_urls": ["https://example.com/photo.png"],
"mask_url": "https://example.com/mask.png"
}'
const response = await fetch('https://toapis.com/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-ToAPIs-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-image-2-official',
prompt: '星空下的古老城堡,電影感光影,超寫實風格',
size: '16:9',
resolution: '2k',
quality: 'high',
n: 1
})
});
const task = await response.json();
console.log(task.id, task.status);
{
"id": "tsk_img_01KPTXXXXXXXXXXXXXXX",
"object": "generation.task",
"model": "gpt-image-2-official",
"status": "queued",
"progress": 0,
"created_at": 1703884800,
"metadata": {}
}
中國大陸用戶請注意: 中國大陸用戶請使用
https://toapis.cn 作為接口地址(Base URL)。文檔示例中的 https://toapis.com 請替換為 https://toapis.cn。- OpenAI 官方
gpt-image-2-official模型 - 異步處理模式,返回任務 ID 用於後續查詢
- 支持文生圖、多參考圖圖生圖、遮罩局部重繪(inpainting)
- 支持 13 種寬高比,可選 1K / 2K / 4K 三檔分辨率
- 單次最多生成 4 張圖,參考圖最多 16 張
Authorizations
string
必填
所有接口均需要使用 Bearer Token 進行認證獲取 API Key:訪問 API Key 管理頁面 獲取您的 API Key使用時在請求頭中添加:
Authorization: Bearer YOUR_API_KEY
Body
string
預設值:"gpt-image-2-official"
必填
圖像生成模型名稱固定填寫
gpt-image-2-officialstring
必填
圖像生成的文本描述支持中英文,建議詳細描述場景、風格和構圖
string
預設值:"1:1"
畫面寬高比支持以下預設比例,也可傳
auto 由上游自動選擇:1:1 · 3:2 · 2:3 · 4:3 · 3:4 · 5:4 · 4:5 · 16:9 · 9:16 · 2:1 · 1:2 · 21:9 · 9:21也支持使用 寬:高 格式傳入任意比例,例如 1:3、7:4,但必須符合下方的尺寸限制。使用 auto 時,最終寬高比和像素尺寸由上游決定,無法保證固定輸出。string
預設值:"1k"
分辨率檔位
1k— 1024 基準,速度快,日常夠用(默認)2k— 2048 基準,適合海報 / 高清需求4k— 3840 基準,high質量下耗時可能超過 120 秒
尺寸對照表
| size | 1K | 2K | 4K |
|---|---|---|---|
1:1 | 1024x1024 | 2048x2048 | 2880x2880 |
3:2 | 1536x1024 | 2048x1360 | 3520x2336 |
2:3 | 1024x1536 | 1360x2048 | 2336x3520 |
4:3 | 1024x768 | 2048x1536 | 3312x2480 |
3:4 | 768x1024 | 1536x2048 | 2480x3312 |
5:4 | 1280x1024 | 2560x2048 | 3216x2576 |
4:5 | 1024x1280 | 2048x2560 | 2576x3216 |
16:9 | 1536x864 | 2048x1152 | 3840x2160 |
9:16 | 864x1536 | 1152x2048 | 2160x3840 |
2:1 | 2048x1024 | 2688x1344 | 3840x1920 |
1:2 | 1024x2048 | 1344x2688 | 1920x3840 |
21:9 | 2016x864 | 2688x1152 | 3840x1648 |
9:21 | 864x2016 | 1152x2688 | 1648x3840 |
任意分辨率
除上述預設比例外,可以通過size 的 寬:高 格式請求任意比例。服務端會根據 resolution 檔位計算實際像素尺寸,結果必須滿足:
- 寬和高都必須是 16 像素的倍數
- 長邊最大可達 3,840 像素(4K)
- 寬高比最大可達 3:1
- 像素總數範圍為 655,360–8,294,400
size: "1:3"、resolution: "2k"→1024x3072size: "7:4"、resolution: "1k"→1344x768
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "白色背景上的紅色圓形,簡潔測試圖",
"size": "7:4",
"resolution": "1k",
"quality": "medium",
"n": 1
}'
string
預設值:"high"
圖片質量
low— 快速省錢,適合草稿/預覽medium— 平衡速度與質量high— 最高精度,默認值(4K + high 耗時可達 120s+)
string
預設值:"auto"
生成圖像的背景樣式
auto— 由模型自動決定(默認)transparent— 透明背景opaque— 不透明背景
透明背景建議搭配 PNG 輸出使用。
string
預設值:"png"
輸出格式
png— 默認jpeg— 文件更小(支持壓縮)
Azure OpenAI 不支持
webp 格式。integer
預設值:100
JPEG 壓縮強度,範圍
0–100(0 不壓縮,100 最大壓縮,默認 100)僅對 output_format: "jpeg" 有效integer
預設值:1
生成圖片張數取值範圍:
1 ~ 10string
遮罩圖 URL,用於局部重繪(inpainting)需搭配
image_urls 使用,遮罩圖尺寸須與首張參考圖一致,且需包含 Alpha 通道(透明區域爲待重繪區域)Response
string
任務唯一標識符,用於查詢任務狀態
string
對象類型,固定爲
generation.taskstring
使用的模型名稱
string
任務狀態
queued— 排隊等待處理in_progress— 處理中completed— 成功完成failed— 失敗
integer
任務進度百分比(0-100)
integer
任務創建時間戳(Unix 時間戳)
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "星空下的古老城堡,電影感光影,超寫實風格",
"size": "16:9",
"resolution": "2k",
"quality": "high",
"n": 1
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "雪山日出全景,極致細節",
"size": "16:9",
"resolution": "4k",
"quality": "high",
"output_format": "jpeg",
"output_compression": 90
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "將兩張參考圖融合成一張插畫海報,保留主體輪廓",
"size": "1:1",
"quality": "high",
"image_urls": [
"https://example.com/input-a.png",
"https://example.com/input-b.png"
]
}'
curl --request POST \
--url https://toapis.com/v1/images/generations \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-image-2-official",
"prompt": "把背景換成沙漠日落",
"size": "1:1",
"quality": "medium",
"image_urls": ["https://example.com/photo.png"],
"mask_url": "https://example.com/mask.png"
}'
const response = await fetch('https://toapis.com/v1/images/generations', {
method: 'POST',
headers: {
'Authorization': 'Bearer your-ToAPIs-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'gpt-image-2-official',
prompt: '星空下的古老城堡,電影感光影,超寫實風格',
size: '16:9',
resolution: '2k',
quality: 'high',
n: 1
})
});
const task = await response.json();
console.log(task.id, task.status);
{
"id": "tsk_img_01KPTXXXXXXXXXXXXXXX",
"object": "generation.task",
"model": "gpt-image-2-official",
"status": "queued",
"progress": 0,
"created_at": 1703884800,
"metadata": {}
}