DashX-API / plugins /removebg.js
HerzaJ's picture
Create removebg.js
2a4893b verified
const axios = require('axios');
async function removebg(img_url) {
const uploadResponse = await axios.get('https://aibackgroundremover.org/api/get-upload-url');
const { uploadUrl, publicUrl } = uploadResponse.data;
const imageResponse = await axios.get(img_url, { responseType: 'arraybuffer' });
await axios.put(uploadUrl, imageResponse.data, {
headers: {
'Content-Type': 'image/png',
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
}
});
const removeBgResponse = await axios.post('https://aibackgroundremover.org/api/remove-bg', {
image: publicUrl
}, {
headers: {
'Content-Type': 'application/json',
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
}
});
const { id } = removeBgResponse.data;
while (true) {
const statusResponse = await axios.get(`https://aibackgroundremover.org/api/check-status?id=${id}`, {
headers: {
'Referer': 'https://aibackgroundremover.org/?utm_source=chatgpt.com'
}
});
const { status, output, error } = statusResponse.data;
if (status === 'succeeded') {
return output;
} else if (status === 'failed') {
throw new Error(error || 'Background removal failed');
}
await new Promise(resolve => setTimeout(resolve, 2000));
}
}
const handler = async (req, res) => {
try {
const { url } = req.query;
if (!url) {
return res.status(400).json({
success: false,
error: 'Missing required parameter: url'
});
}
const result = await removebg(url);
res.json({
success: true,
data: {
original_url: url,
output_url: result
}
});
} catch (error) {
res.status(500).json({
success: false,
error: error.message
});
}
};
module.exports = {
name: 'Background Remover',
description: 'Remove background from images using AI',
type: 'GET',
routes: ['api/tools/removebg'],
tags: ['image', 'ai', 'background'],
parameters: ['url', 'key'],
enabled: true,
main: ['tools'],
handler
};