<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Professional Background Remover</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: #f0f2f5;
min-height: 100vh;
}
.container {
max-width: 1200px;
margin: 2rem auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 2rem;
}
.upload-section {
background: white;
border-radius: 10px;
padding: 2rem;
box-shadow: 0 2px 15px rgba(0,0,0,0.1);
margin-bottom: 2rem;
}
.drop-zone {
border: 2px dashed #ccc;
border-radius: 8px;
padding: 3rem;
text-align: center;
cursor: pointer;
transition: all 0.3s;
}
.drop-zone:hover {
border-color: #2196F3;
background: #f8f9fa;
}
.preview-container {
display: none;
margin-top: 2rem;
position: relative;
}
#previewImage {
max-width: 100%;
max-height: 500px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.background-menu {
display: none;
margin-top: 2rem;
background: white;
padding: 1.5rem;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.background-thumbnails {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.background-thumb {
width: 100px;
height: 100px;
border-radius: 8px;
cursor: pointer;
transition: transform 0.2s;
border: 2px solid transparent;
}
.background-thumb:hover {
transform: scale(1.05);
}
.selected-background {
border-color: #2196F3;
}
.loading {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid #2196F3;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.download-btn {
display: none;
margin-top: 1rem;
padding: 12px 24px;
background: #2196F3;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
.download-btn:hover {
background: #1976D2;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Professional Background Remover</h1>
<p>Upload your image to remove background instantly</p>
</div>
<div class="upload-section">
<div class="drop-zone" ondragover="handleDragOver(event)" ondrop="handleDrop(event)">
<p>Drag & Drop image here or <input type="file" id="fileInput" accept="image/*"> click to upload</p>
</div>
<div class="preview-container">
<img id="previewImage" alt="Preview">
<div class="loading">
<div class="spinner"></div>
</div>
</div>
<button class="download-btn" id="downloadBtn">Download Image</button>
</div>
<div class="background-menu">
<h3>Select Background:</h3>
<div class="background-thumbnails">
<!-- Add your background image URLs here -->
<img src="https://example.com/bg1.jpg" class="background-thumb" onclick="selectBackground(this)">
<img src="https://example.com/bg2.jpg" class="background-thumb" onclick="selectBackground(this)">
<img src="https://example.com/bg3.jpg" class="background-thumb" onclick="selectBackground(this)">
</div>
</div>
</div>
<script>
const API_KEY = 'YOUR_REMOVE_BG_API_KEY';
let processedImage = null;
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
function handleDragOver(e) {
e.preventDefault();
e.target.style.borderColor = '#2196F3';
}
function handleDrop(e) {
e.preventDefault();
const files = e.dataTransfer.files;
if (files.length > 0) {
handleImage(files[0]);
}
}
function handleFileSelect(e) {
const file = e.target.files[0];
if (file) {
handleImage(file);
}
}
async function handleImage(file) {
document.querySelector('.preview-container').style.display = 'block';
document.querySelector('.loading').style.display = 'block';
const formData = new FormData();
formData.append('image_file', file);
formData.append('size', 'auto');
try {
const response = await fetch('https://api.remove.bg/v1.0/removebg', {
method: 'POST',
headers: {
'X-Api-Key': API_KEY
},
body: formData
});
if (!response.ok) throw new Error('Background removal failed');
const blob = await response.blob();
processedImage = URL.createObjectURL(blob);
document.getElementById('previewImage').src = processedImage;
document.querySelector('.background-menu').style.display = 'block';
document.getElementById('downloadBtn').style.display = 'inline-block';
} catch (error) {
alert(error.message);
} finally {
document.querySelector('.loading').style.display = 'none';
}
}
function selectBackground(element) {
document.querySelectorAll('.background-thumb').forEach(thumb => {
thumb.classList.remove('selected-background');
});
element.classList.add('selected-background');
// Create composite image with selected background
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const bgImage = new Image();
bgImage.crossOrigin = 'anonymous';
bgImage.src = element.src;
const fgImage = new Image();
fgImage.src = processedImage;
bgImage.onload = () => {
canvas.width = bgImage.width;
canvas.height = bgImage.height;
ctx.drawImage(bgImage, 0, 0);
ctx.drawImage(fgImage, 0, 0, bgImage.width, bgImage.height);
document.getElementById('previewImage').src = canvas.toDataURL();
};
}
document.getElementById('downloadBtn').addEventListener('click', () => {
const link = document.createElement('a');
link.download = 'background-removed.png';
link.href = processedImage;
link.click();
});
</script>
</body>
</html>