background remover

 <!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Background Remover Tool</title>

  <style>

    body {

      font-family: Arial, sans-serif;

      text-align: center;

      margin: 20px;

    }

    #upload-container {

      margin: 20px auto;

      max-width: 400px;

      padding: 20px;

      border: 2px dashed #ccc;

      border-radius: 10px;

    }

    #image-preview {

      max-width: 100%;

      display: none;

      margin-top: 20px;

    }

    #download-link {

      display: none;

      margin-top: 20px;

      color: #fff;

      background-color: #007bff;

      padding: 10px 20px;

      border-radius: 5px;

      text-decoration: none;

    }

    #download-link:hover {

      background-color: #0056b3;

    }

  </style>

</head>

<body>

  <h1>Background Remover Tool</h1>

  <p>Upload an image to remove its background:</p>


  <div id="upload-container">

    <input type="file" id="image-input" accept="image/*" />

    <br><br>

    <img id="image-preview" src="#" alt="Image Preview" />

    <a id="download-link" href="#" download="background-removed.png">Download Image</a>

  </div>


  <script>

    const imageInput = document.getElementById('image-input');

    const imagePreview = document.getElementById('image-preview');

    const downloadLink = document.getElementById('download-link');


    imageInput.addEventListener('change', function (event) {

      const file = event.target.files[0];

      if (file) {

        const reader = new FileReader();

        reader.onload = function (e) {

          imagePreview.src = e.target.result;

          imagePreview.style.display = 'block';

          removeBackground(file);

        };

        reader.readAsDataURL(file);

      }

    });


    async function removeBackground(imageFile) {

      const apiKey = 'YOUR_REMOVE_BG_API_KEY'; // Replace with your Remove.bg API key

      const formData = new FormData();

      formData.append('image_file', imageFile);


      try {

        const response = await fetch('https://api.remove.bg/v1.0/removebg', {

          method: 'POST',

          headers: {

            'X-Api-Key': apiKey,

          },

          body: formData,

        });


        if (!response.ok) {

          throw new Error('Failed to remove background');

        }


        const blob = await response.blob();

        const url = URL.createObjectURL(blob);

        downloadLink.href = url;

        downloadLink.style.display = 'inline-block';

      } catch (error) {

        alert('Error: ' + error.message);

      }

    }

  </script>

</body>

</html>

Comments