Create removebg.js
Browse files- plugins/removebg.js +85 -0
plugins/removebg.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require('axios');
|
| 2 |
+
|
| 3 |
+
async function removebg(img_url) {
|
| 4 |
+
const uploadResponse = await axios.get('https://aibackgroundremover.org/api/get-upload-url');
|
| 5 |
+
const { uploadUrl, publicUrl } = uploadResponse.data;
|
| 6 |
+
|
| 7 |
+
const imageResponse = await axios.get(img_url, { responseType: 'arraybuffer' });
|
| 8 |
+
|
| 9 |
+
await axios.put(uploadUrl, imageResponse.data, {
|
| 10 |
+
headers: {
|
| 11 |
+
'Content-Type': 'image/png',
|
| 12 |
+
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
|
| 13 |
+
}
|
| 14 |
+
});
|
| 15 |
+
|
| 16 |
+
const removeBgResponse = await axios.post('https://aibackgroundremover.org/api/remove-bg', {
|
| 17 |
+
image: publicUrl
|
| 18 |
+
}, {
|
| 19 |
+
headers: {
|
| 20 |
+
'Content-Type': 'application/json',
|
| 21 |
+
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
const { id } = removeBgResponse.data;
|
| 26 |
+
|
| 27 |
+
while (true) {
|
| 28 |
+
const statusResponse = await axios.get(`https://aibackgroundremover.org/api/check-status?id=${id}`, {
|
| 29 |
+
headers: {
|
| 30 |
+
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
|
| 31 |
+
}
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
const { status, output, error } = statusResponse.data;
|
| 35 |
+
|
| 36 |
+
if (status === 'succeeded') {
|
| 37 |
+
return output;
|
| 38 |
+
} else if (status === 'failed') {
|
| 39 |
+
throw new Error(error || 'Background removal failed');
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
const handler = async (req, res) => {
|
| 47 |
+
try {
|
| 48 |
+
const { url } = req.query;
|
| 49 |
+
|
| 50 |
+
if (!url) {
|
| 51 |
+
return res.status(400).json({
|
| 52 |
+
success: false,
|
| 53 |
+
error: 'Missing required parameter: url'
|
| 54 |
+
});
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
const result = await removebg(url);
|
| 58 |
+
|
| 59 |
+
res.json({
|
| 60 |
+
success: true,
|
| 61 |
+
data: {
|
| 62 |
+
original_url: url,
|
| 63 |
+
output_url: result
|
| 64 |
+
}
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
} catch (error) {
|
| 68 |
+
res.status(500).json({
|
| 69 |
+
success: false,
|
| 70 |
+
error: error.message
|
| 71 |
+
});
|
| 72 |
+
}
|
| 73 |
+
};
|
| 74 |
+
|
| 75 |
+
module.exports = {
|
| 76 |
+
name: 'Background Remover',
|
| 77 |
+
description: 'Remove background from images using AI',
|
| 78 |
+
type: 'GET',
|
| 79 |
+
routes: ['api/tools/removebg'],
|
| 80 |
+
tags: ['image', 'ai', 'background'],
|
| 81 |
+
parameters: ['url', 'key'],
|
| 82 |
+
enabled: true,
|
| 83 |
+
main: ['tools'],
|
| 84 |
+
handler
|
| 85 |
+
};
|