File size: 2,094 Bytes
2a4893b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
};