-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
171 lines (144 loc) · 7.69 KB
/
index.html
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Background Remover by Saif Saidi</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
<div class="bg-white p-8 rounded-xl shadow-2xl max-w-4xl w-full">
<h1 class="text-4xl font-bold mb-6 text-center text-gray-800">Background Remover</h1>
<p class="text-center text-gray-600 mb-8">
<a href="/about.html" class="underline hover:text-blue-600 transition-colors">by Saif Saidi</a>
</p>
<form id="uploadForm" class="space-y-8">
<div class="flex flex-col items-center space-y-4">
<input type="file" id="fileInput" accept="image/*" multiple class="hidden" />
<label for="fileInput"
class="cursor-pointer bg-blue-500 text-white px-6 py-3 rounded-lg hover:bg-blue-600 transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50">
Choose Images
</label>
<p id="fileCount" class="text-gray-600"></p>
<button type="button" id="removeBgButton"
class="bg-gradient-to-r from-purple-600 to-blue-500 text-white px-6 py-3 rounded-lg hover:from-purple-500 hover:to-blue-600 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-opacity-50 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
disabled>
Remove Background
</button>
</div>
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<div>
<label for="format" class="block text-gray-700 font-semibold mb-2">Export Format:</label>
<select id="format"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
<option value="webp">WebP</option>
<option value="png">PNG</option>
<option value="jpeg">JPEG</option>
</select>
</div>
<div>
<label for="quality" class="block text-gray-700 font-semibold mb-2">Quality:</label>
<select id="quality"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
<option value="high">High</option>
<option value="medium">Medium</option>
<option value="low">Low</option>
</select>
</div>
<div>
<label for="filename" class="block text-gray-700 font-semibold mb-2">Output Filename:</label>
<input type="text" id="filename"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="output">
</div>
</div>
</form>
<div id="imagePreviews" class="mt-8 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6">
</div>
</div>
<script>
const fileInput = document.getElementById("fileInput");
const removeBgButton = document.getElementById("removeBgButton");
const imagePreviews = document.getElementById("imagePreviews");
const formatSelect = document.getElementById("format");
const qualitySelect = document.getElementById("quality");
const filenameInput = document.getElementById("filename");
const fileCountDisplay = document.getElementById("fileCount");
let selectedFiles = [];
fileInput.addEventListener("change", () => {
selectedFiles = Array.from(fileInput.files);
removeBgButton.disabled = selectedFiles.length === 0;
fileCountDisplay.textContent = `${selectedFiles.length} file(s) selected`;
updateImagePreviews();
});
function updateImagePreviews() {
imagePreviews.innerHTML = '';
selectedFiles.forEach((file, index) => {
const previewContainer = document.createElement('div');
previewContainer.className = 'flex flex-col items-center space-y-2';
const img = document.createElement('img');
img.src = URL.createObjectURL(file);
img.alt = `Preview ${index + 1}`;
img.className = 'w-full h-48 object-cover rounded-lg shadow-md';
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.className = 'text-red-500 hover:text-red-700';
removeButton.onclick = () => removeFile(index);
previewContainer.appendChild(img);
previewContainer.appendChild(removeButton);
imagePreviews.appendChild(previewContainer);
});
}
function removeFile(index) {
selectedFiles.splice(index, 1);
updateImagePreviews();
removeBgButton.disabled = selectedFiles.length === 0;
fileCountDisplay.textContent = `${selectedFiles.length} file(s) selected`;
}
removeBgButton.addEventListener("click", async () => {
const format = formatSelect.value;
const quality = qualitySelect.value;
const filename = filenameInput.value || "output";
if (selectedFiles.length === 0) return;
removeBgButton.disabled = true;
removeBgButton.textContent = "Processing...";
try {
for (let i = 0; i < selectedFiles.length; i++) {
const file = selectedFiles[i];
const formData = new FormData();
formData.append("image", file);
formData.append("format", format);
formData.append("quality", quality);
const response = await fetch("http://localhost:3000/api/remove-bg", {
method: "POST",
body: formData
});
if (!response.ok) {
throw new Error(`Failed to process image ${i + 1}.`);
}
const blob = await response.blob();
const outputURL = URL.createObjectURL(blob);
const previewContainer = imagePreviews.children[i];
const processedImg = document.createElement('img');
processedImg.src = outputURL;
processedImg.alt = `Processed ${i + 1}`;
processedImg.className = 'w-full h-48 object-cover rounded-lg shadow-md';
const downloadLink = document.createElement('a');
downloadLink.href = outputURL;
downloadLink.download = `${filename}_${i + 1}.${format}`;
downloadLink.textContent = 'Download';
downloadLink.className = 'text-blue-500 hover:text-blue-700';
previewContainer.innerHTML = '';
previewContainer.appendChild(processedImg);
previewContainer.appendChild(downloadLink);
}
} catch (error) {
alert(error.message);
} finally {
removeBgButton.disabled = false;
removeBgButton.textContent = "Remove Background";
}
});
</script>
</body>
</html>