AI 图片换背景接口
基于服务器本地开源模型 rembg/U²-Net,无需第三方 API Key。该接口独立计费,最多支持 9 张主图并发处理。
接口地址
POST /api/ai_bg/index.php
该接口必须使用 POST 请求,并提交 JSON 数据。不要直接在浏览器地址栏打开接口地址,否则会返回 405 仅支持POST请求。
服务器依赖安装
AI 换背景依赖 rembg 和 onnxruntime,服务器 Python 必须为 3.8 或更高版本。你的系统默认 Python 3.6.8 过旧,无法安装 rembg,请不要继续用系统 /usr/bin/python3 安装。
cd /www/wwwroot/您的站点目录
python3.8 --version
python3.8 -m pip install --upgrade pip setuptools wheel
python3.8 -m pip install -r ai_tools/requirements.txt
如果新 Python 不叫 python3.8,请把环境变量 AI_BG_PYTHON_BIN 设置为新 Python 的完整路径,例如 /usr/local/bin/python3.8。
请求参数
| 参数 | 必填 | 说明 |
apikey | 是 | 您的 API 密钥 |
main_images | 是 | 主图链接数组,最多 9 张 |
background_images | 是 | 背景图链接数组,可传 1 张或最多 9 张 |
mode | 否 | fit 保留主体完整;cover 铺满画布,默认 fit |
YOUR_API_KEY 请替换为用户中心「API密钥」页面生成的完整密钥,不是登录密码,也不是密钥名称;复制时不要带空格或省略号。
请求示例
curl -X POST "/api/ai_bg/index.php" \
-H "Content-Type: application/json" \
-d '{
"apikey": "YOUR_API_KEY",
"main_images": ["https://example.com/main.jpg"],
"background_images": ["https://example.com/bg.jpg"],
"mode": "fit"
}'
Authorization Header 请求示例
curl -X POST "/api/ai_bg/index.php" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{
"main_images": ["https://example.com/main.jpg"],
"background_images": ["https://example.com/bg.jpg"],
"mode": "fit"
}'
JavaScript Fetch 请求示例
fetch(`${location.origin}/api/ai_bg/index.php`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'YOUR_API_KEY'
},
body: JSON.stringify({
main_images: ['https://example.com/main.jpg'],
background_images: ['https://example.com/bg.jpg'],
mode: 'fit'
})
})
.then(res => res.json())
.then(console.log);
成功响应
{
"code": 200,
"msg": "success",
"data": {
"type": "local_open_source_background_replace",
"engine": "rembg/u2net",
"billing": {
"cost_per_main_image": 5,
"requested_main_images": 1,
"success_count": 1,
"charged": 5
},
"results": [
{
"index": 0,
"status": "success",
"image_url": "https://当前域名/uploads/ai_bg/20260508/result.png"
}
]
}
}
图片上传转公网链接接口
上传本地图片到服务器,返回可直接访问的公网 URL。适合 AI 换背景前置传图、图床引用、临时素材分发等场景。每次成功上传消耗 1 次余额。
接口地址
POST /api/upload/index.php
请求类型
multipart/form-data
请求参数
| 参数 | 必填 | 说明 |
apikey | 是 | 您的 API 密钥 |
image | 是 | 图片文件,支持 JPG、PNG、WEBP、GIF,最大 10MB |
请求示例
curl -X POST "/api/upload/index.php" \
-F "apikey=YOUR_API_KEY" \
-F "image=@/path/to/image.png"
成功响应
{
"code": 200,
"msg": "success",
"data": {
"id": 12,
"url": "https://当前域名/uploads/images/20260509/120000_xxxxxxxx.png",
"filename": "120000_xxxxxxxx.png",
"mime": "image/png",
"size": 204800,
"charged": 1
}
}
响应说明
成功响应(视频内容)
{
"code": 200,
"msg": "success",
"data": {
"title": "视频标题",
"cover": "视频封面图片URL",
"video_url": "无水印视频直链",
"images": [],
"music_url": "背景音乐URL",
"author": "作者昵称",
"platform": "douyin",
"type": "video"
}
}
成功响应(图文内容)
{
"code": 200,
"msg": "success",
"data": {
"title": "图文标题",
"cover": "封面图片URL",
"video_url": "",
"images": [
"图片1URL",
"图片2URL",
"图片3URL"
],
"music_url": "",
"author": "作者昵称",
"platform": "xiaohongshu",
"type": "images"
}
}
响应字段说明
| 字段 | 类型 | 说明 |
| code | int | 状态码,200表示成功 |
| msg | string | 状态说明 |
| data.title | string | 内容标题 |
| data.cover | string | 封面图URL |
| data.video_url | string | 无水印视频直链(视频类型时有值) |
| data.images | array | 图片URL列表(图文类型时有值) |
| data.music_url | string | 背景音乐URL(部分平台支持) |
| data.author | string | 作者昵称 |
| data.platform | string | 平台标识(douyin/kuaishou等) |
| data.type | string | 内容类型:video(视频)或 images(图文) |
代码示例
PHP
<?php
$apikey = 'YOUR_API_KEY';
$url = 'https://v.douyin.com/Mncpi-JnoVI/';
$apiUrl = '/api/qsy/index.php?' . http_build_query([
'apikey' => $apikey,
'url' => $url,
]);
$result = json_decode(file_get_contents($apiUrl), true);
if ($result['code'] === 200) {
echo '视频标题: ' . $result['data']['title'] . "\n";
echo '无水印地址: ' . $result['data']['video_url'] . "\n";
} else {
echo '解析失败: ' . $result['msg'];
}
Python
import requests
apikey = 'YOUR_API_KEY'
url = 'https://v.douyin.com/Mncpi-JnoVI/'
api_url = f'/api/qsy/index.php?apikey={apikey}&url={url}'
resp = requests.get(api_url)
data = resp.json()
if data['code'] == 200:
print(f"视频标题: {data['data']['title']}")
print(f"无水印地址: {data['data']['video_url']}")
else:
print(f"解析失败: {data['msg']}")
JavaScript
const apikey = 'YOUR_API_KEY';
const url = 'https://v.douyin.com/Mncpi-JnoVI/';
const apiUrl = `${location.origin}/api/qsy/index.php?apikey=${apikey}&url=${encodeURIComponent(url)}`;
fetch(apiUrl)
.then(res => res.json())
.then(data => {
if (data.code === 200) {
console.log('视频标题:', data.data.title);
console.log('无水印地址:', data.data.video_url);
} else {
console.log('解析失败:', data.msg);
}
});