概览
JavaScript 背景移除 API
JavaScript 使现代 Web 应用能够轻松集成图片上传和 AI 图像处理功能。
STEP 1
<input type="file"
id="imageInput">
<button onclick="removeBg()">
Remove Background
</button>
<div id="result"></div>
STEP 2
Fetch API 示例
发送图片并接收透明 PNG 结果。
async function removeBg(){
const fileInput =
document.getElementById("imageInput");
const result =
document.getElementById("result");
if(!fileInput.files.length){
alert("Select image first");
return;
}
const formData = new FormData();
formData.append(
"file",
fileInput.files[0]
);
result.innerHTML =
"Processing...";
try{
const response = await fetch(
"/ajax/remove-bg.php",
{
method:"POST",
body:formData
}
);
if(!response.ok){
throw new Error();
}
const blob =
await response.blob();
const url =
URL.createObjectURL(blob);
result.innerHTML = `
<img src="${url}"
style="max-width:100%;">
<br><br>
<a href="${url}"
download="no-bg.png">
Download PNG
</a>
`;
}catch{
result.innerHTML =
"Something went wrong";
}
}
FEATURES
推荐功能
SECURITY
重要提示
ERRORS
Error
Description
401
413
429
500
继续学习 Python
了解如何使用 Python 和 ByByBG API 自动化背景移除流程。