基于 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 replace3. 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 scenePlacement 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 5API 参考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 - 强大的图像生成模型
基于 nano banana2 实现的 AI 图像精确 P 图与蒙板替换技术( PHP 代码示例)
内容版权声明:除非注明,否则皆为本站原创文章。