基于
Dataler.com API 的 AI 图像精确 P 图实现,使用 MASK 蒙板技术实现像素级精确的产品替换。
项目发布到了 GITHUB,链接:
https://github.com/lmj243-code/dataler-ai-image-inpaintingPHP License
平台推荐
Dataler.com - 专业的第三方 AI 中转 API 平台//官方 2.2 折价格,大幅降低 AI 图像生成成本
对接几乎所有主流 AI 图像生成模型
动态负载自适应,智能调度确保稳定高效
兼容 Gemini API 格式,无缝迁移现有项目
API 端点:
https://dataler.com/v1beta/models/{model}:generateContent
核心功能
1. AI 反推 Prompt
自动分析图片生成详细的 AI 图像生成提示词
2. 垫图换产品生图( Prompt 替换模式)
分析垫图产品特征
智能融合 Prompt
生成新图
3. 原图产品换垫图产品(双图融合模式)
反推场景图(人物、光线、氛围)
反推产品图(外观、材质、颜色)
智能整合生成
4. 精确 P 图 - MASK 蒙板替换模式 ⭐
这是最精确的图像替换技术
技术原理与流程
MASK 蒙板替换工作流程
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 垫图(场景图) │ │ 产品图(新) │ │ 用户描述(可选) │
└────────┬────────┘ └────────┬────────┘ └────────┬────────┘
│ │ │
▼ │ │
┌─────────────────┐ │ │
│ AI 生成 MASK 蒙板 │◄─────────────┴───────────────────────┘
│ 白色=替换区域 │ (根据用户描述或自动识别主体)
│ 黑色=保留区域 │
└────────┬────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Inpainting 生成阶段 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 原图场景 │ │ MASK 蒙板 │ │ 新产品图 │ │
│ │ (保留) │ │ (黑白) │ │ (替换来源) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └──────────────┼──────────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ AI Inpainting │ │
│ │ 精确替换生成 │ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────┘
详细步骤
步骤 1:图片准备
读取垫图(场景图)和新产品图
智能压缩(最大边 1500px ,保持质量同时减少传输)
转换为 Base64 格式
步骤 2:AI 生成 MASK 蒙板
根据用户是否提供目标描述,采用不同策略:
策略 A - 用户指定替换目标:
Prompt 示例:
"Generate an image: 请仔细观察这张图片,为 inpainting 创建一张精确的黑白蒙版(MASK)图。
[要做成白色蒙版的目标区域]
[用户描述的目标物品]
请把上述描述的所有内容(包括它们占据的完整区域)都涂成纯白色(#FFFFFF)。
图片中其他所有内容(背景、墙壁、地板、人物、文字、其他不相关的物品)都涂成纯黑色(#000000)。
Rules:
- The MASK must be the EXACT SAME dimensions as the original image
- WHITE (#FFFFFF) = the target area described above (to be replaced)
- BLACK (#000000) = everything else (to be kept)
- Cover the ENTIRE target area including all parts mentioned in the description
- Use smooth edges with a small margin (3-5 pixels) around the target
- Clean black and white only, NO gray, NO gradients
- Do NOT include shadows or reflections in the white area
- Output ONLY the mask image, no text."
策略 B - 自动识别主体:
Prompt 示例:
"Generate an image: Look at this image carefully. Create a precise MASK image for inpainting.
The MASK must be the EXACT SAME dimensions as the original image.
Identify the MAIN PRODUCT/SUBJECT in the image and mask it.
Rules:
- Paint the MAIN PRODUCT/SUBJECT area in PURE WHITE (#FFFFFF)
- Paint EVERYTHING ELSE in PURE BLACK (#000000)
- Cover the product outline with a small margin (3-5 pixels)
- Use smooth edges, no jagged borders
- Clean black and white only, NO gray, NO gradients
- Do NOT include shadows in the white area
- Output ONLY the mask image, no text."
步骤 3:反推产品外观特征
AI 分析新产品图,提取:
整体形状和轮廓
尺寸比例
精确颜色(深空灰、象牙白、玫瑰金等)
材质质感(金属/塑料/木材/玻璃/布料,哑光/亮面/磨砂)
表面细节(纹理、图案、反光特性、logo 位置)
结构特征(按钮、接口、把手、铰链、缝线)
产品数量和排列
步骤 4:Inpainting 精确替换
构建包含四部分的请求:
文本指令:详细的替换规则说明
原图场景:作为背景保留
MASK 蒙板:黑白图,白色区域将被替换
新产品图:替换来源
关键指令模板:
"Generate an image: I am providing three images:
1. The FIRST image is the original photo (the scene/background to keep)
2. The SECOND image is a black-and-white MASK where WHITE areas indicate the region to replace
3. The THIRD image is the new product/object that should be placed into the white masked area
**[PRODUCT APPEARANCE REFERENCE - from the THIRD image]**
[反推的产品外观描述]
**[CRITICAL - PRODUCT FIDELITY RULES]**
The product from the THIRD image must be reproduced with 100% visual fidelity:
- EXACT original shape, proportions, and aspect ratio — NO stretching, squishing, warping, or distortion
- EXACT original colors, materials, textures, surface details, logos, and text
- EXACT original structural features (buttons, handles, edges, curves, patterns)
- Scale the product uniformly to fit the masked area — maintain width-to-height ratio strictly
- If the masked area is a different shape than the product, fit the product within the area with appropriate background fill — do NOT deform the product to fill the mask
- The product in the result must look like an exact copy of the THIRD image, just placed into a new scene
Placement rules:
- Adjust ONLY the viewing angle slightly to match the scene perspective
- Match the scene lighting direction and color temperature on the product surface
- Add natural shadows consistent with the scene light source
- Blend edges seamlessly with the surrounding area
- Keep ALL black masked areas (background, people, environment) EXACTLY unchanged
- Preserve the exact resolution and aspect ratio of the original image"
技术要点
蒙板生成要点
纯黑白:不允许灰色或渐变
边缘平滑:3-5 像素的过渡边距
不包含阴影:白色区域仅包含产品本身
尺寸一致:MASK 必须与原图尺寸完全相同
产品保真要点
形状不变:禁止拉伸、压缩、变形
比例保持:宽高比严格保持
材质还原:颜色、纹理、反光特性 100%还原
结构完整:所有可见部件必须保留
场景融合要点
透视匹配:根据场景调整产品视角
光影一致:匹配场景光源方向和色温
阴影自然:添加符合光源的阴影
边缘融合:与周围环境无缝衔接
应用场景
电商产品替换:模特手持产品图,快速替换不同款式
场景营销图:保持精美场景,更换展示产品
广告素材制作:批量生成同一产品的不同场景展示
产品迭代展示:同一角度展示产品不同配色/配置
虚拟试穿/试用:将产品自然融入用户场景
快速开始
环境要求
PHP 8.0+
cURL 扩展
GD 扩展(用于图片处理)
安装
git clone
https://github.com/lmj243-code/dataler-ai-image-inpainting.gitcd dataler-ai-image-inpainting
使用方法
1. 基本用法(自动识别主体)
require_once 'DatalerInpaintingAPI.php';
$apiKey = 'your-api-key-here';
$api = new DatalerInpaintingAPI($apiKey);
$result = $api->replaceProductWithMask(
'scene.jpg', // 场景图:模特手持旧产品
'new_product.jpg', // 新产品图:要替换进去的产品
null, // 不指定目标,自动识别
'output.png', // 输出路径
true // 启用压缩
);
2. 指定替换目标
$result = $api->replaceProductWithMask(
'model_with_bag.jpg', // 场景图:模特拿着红色手提包
'blue_bag.jpg', // 新产品图:蓝色手提包
'红色手提包', // 明确指定要替换的是红色手提包
'output_blue_bag.png', // 输出路径
true
);
3. 命令行运行示例
# 运行示例 1:自动识别
php DatalerInpaintingAPI.php 1
# 运行示例 2:指定替换目标
php DatalerInpaintingAPI.php 2
# 运行示例 3:分步调用
php DatalerInpaintingAPI.php 3
# 运行示例 4:批量处理
php DatalerInpaintingAPI.php 4
# 运行示例 5:自定义日志
php DatalerInpaintingAPI.php 5
API 参考
DatalerInpaintingAPI 类
构造函数
public function __construct(string $apiKey)
主要方法
方法 说明
replaceProductWithMask() 一键完成完整的蒙板替换流程
generateMask() AI 生成 MASK 蒙板
analyzeProduct() 反推产品外观特征
inpaint() 执行 Inpainting 精确替换
compressImage() 智能图片压缩
imageToBase64() 图片转 Base64
支持的模型
gemini-3-pro-image-preview:专业图像生成模型
gemini-3.1-flash-image-preview:快速图像生成模型
最佳实践
图片质量:建议使用清晰、光线均匀的产品图
描述精确:用户提供的目标描述越详细,MASK 定位越准确
多试几次:AI 生成有一定随机性,不满意可多次尝试
尺寸匹配:场景图和产品图分辨率建议相近
压缩策略:大图片适当压缩可提升 API 响应速度
文件说明
文件 说明
DatalerInpaintingAPI.php 核心 API 类,包含完整实现
GUIDE.md 详细技术文档和原理说明
README.md 项目说明文档
作者
LT - 初始代码和文档
许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件
致谢
Dataler.com - 提供稳定高效的 AI API 服务
Gemini - 强大的图像生成模型