-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
326 lines (284 loc) · 12.8 KB
/
index.html
File metadata and controls
326 lines (284 loc) · 12.8 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Circular Media Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
<style>
:root {
--rotation: 0deg;
}
body {
background: #0f172a;
color: white;
font-family: 'Inter', sans-serif;
overflow-x: hidden;
}
.wheel-container {
position: relative;
width: 350px;
height: 350px;
transition: transform 1s cubic-bezier(0.4, 0, 0.2, 1);
transform: rotate(var(--rotation));
}
.extension-node {
position: absolute;
width: 70px;
height: 70px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
background: #1e293b;
border: 2px solid #3b82f6;
cursor: pointer;
transition: all 0.3s ease;
transform: translate(-50%, -50%);
left: 50%;
top: 50%;
}
.extension-node.active {
background: #3b82f6;
box-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
transform: translate(-50%, -50%) scale(1.2);
}
.node-text {
transform: rotate(calc(-1 * var(--rotation)));
font-weight: bold;
pointer-events: none;
}
.center-action {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 120px;
height: 120px;
background: white;
border-radius: 50%;
color: #0f172a;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 10;
text-align: center;
padding: 10px;
box-shadow: 0 10px 25px rgba(0,0,0,0.3);
}
.hidden-input {
display: none;
}
.status-pulse {
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.05); opacity: 0.8; }
100% { transform: scale(1); opacity: 1; }
}
input[type=range] {
accent-color: #3b82f6;
}
</style>
</head>
<body class="flex flex-col items-center justify-center min-h-screen p-4">
<div class="text-center mb-8">
<h1 class="text-3xl font-bold mb-2 uppercase tracking-widest">Hard Compressor</h1>
<p class="text-slate-400">Strict Limit: 200kb - 900kb</p>
</div>
<!-- Compression Slider -->
<div class="mb-10 w-full max-w-xs bg-slate-800/50 p-4 rounded-2xl border border-slate-700">
<div class="flex justify-between text-xs mb-2 text-slate-400 font-bold uppercase tracking-tighter">
<span>Tight (200kb)</span>
<span id="targetLabel" class="text-blue-400">Limit: 500kb</span>
<span>Max (900kb)</span>
</div>
<input type="range" id="sizeSlider" min="200" max="900" value="500" class="w-full h-2 bg-slate-700 rounded-lg appearance-none cursor-pointer">
<p class="text-[9px] text-center mt-2 text-slate-500 italic">Large files (3MB+) will be auto-downscaled to fit.</p>
</div>
<!-- Interface Wrapper -->
<div class="relative flex items-center justify-center">
<div id="wheel" class="wheel-container">
<!-- Nodes placed via JS -->
</div>
<div id="uploadBtn" class="center-action group hover:bg-blue-50">
<svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 mb-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12" />
</svg>
<span class="text-xs font-bold uppercase tracking-wider" id="actionLabel">Select Format</span>
</div>
</div>
<input type="file" id="fileInput" class="hidden-input" accept=".png,.jpg,.jpeg,.pdf">
<!-- Progress UI -->
<div id="progressArea" class="mt-12 w-full max-w-md hidden">
<div class="bg-slate-800 rounded-xl p-6 shadow-xl border border-slate-700">
<div class="flex justify-between items-center mb-4">
<span id="fileNameDisplay" class="truncate font-medium text-blue-400">file.png</span>
<span id="statusBadge" class="text-xs bg-blue-500/20 text-blue-400 px-2 py-1 rounded">Processing</span>
</div>
<div class="w-full bg-slate-700 rounded-full h-2 mb-2">
<div id="progressBar" class="bg-blue-500 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
</div>
<p id="sizeReductionInfo" class="text-[10px] text-slate-400 mb-4 text-center"></p>
<div id="downloadLinkContainer"></div>
</div>
</div>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.worker.min.js';
const formats = ['JPG', 'PNG', 'PDF'];
const wheel = document.getElementById('wheel');
const uploadBtn = document.getElementById('uploadBtn');
const fileInput = document.getElementById('fileInput');
const actionLabel = document.getElementById('actionLabel');
const progressArea = document.getElementById('progressArea');
const progressBar = document.getElementById('progressBar');
const fileNameDisplay = document.getElementById('fileNameDisplay');
const downloadContainer = document.getElementById('downloadLinkContainer');
const sizeSlider = document.getElementById('sizeSlider');
const targetLabel = document.getElementById('targetLabel');
const sizeInfo = document.getElementById('sizeReductionInfo');
let selectedFormat = null;
let currentRotation = 0;
sizeSlider.oninput = (e) => {
targetLabel.innerText = `Limit: ${e.target.value}kb`;
};
function initWheel() {
const radius = 140;
formats.forEach((fmt, i) => {
const node = document.createElement('div');
node.className = 'extension-node';
node.dataset.format = fmt;
const angle = (i * (360 / formats.length)) * (Math.PI / 180);
const x = 175 + radius * Math.cos(angle);
const y = 175 + radius * Math.sin(angle);
node.style.left = `${x}px`;
node.style.top = `${y}px`;
node.innerHTML = `<span class="node-text">${fmt}</span>`;
node.onclick = () => selectFormat(fmt, i);
wheel.appendChild(node);
});
}
function selectFormat(fmt, index) {
selectedFormat = fmt;
const targetAngle = (index * (360 / formats.length));
currentRotation = -targetAngle - 90;
document.documentElement.style.setProperty('--rotation', `${currentRotation}deg`);
document.querySelectorAll('.extension-node').forEach(n => n.classList.remove('active'));
document.querySelectorAll('.extension-node')[index].classList.add('active');
actionLabel.innerText = `Upload for ${fmt}`;
uploadBtn.classList.add('status-pulse');
}
uploadBtn.onclick = () => {
if (!selectedFormat) return;
fileInput.click();
};
fileInput.onchange = async (e) => {
const file = e.target.files[0];
if (!file) return;
progressArea.classList.remove('hidden');
fileNameDisplay.innerText = file.name;
progressBar.style.width = '10%';
downloadContainer.innerHTML = '';
sizeInfo.innerText = "Applying hard compression limits...";
document.getElementById('statusBadge').innerText = 'Processing';
try {
let sourceData;
if (file.type === 'application/pdf') {
sourceData = await pdfToImageData(file);
} else {
sourceData = await fileToImageData(file);
}
progressBar.style.width = '40%';
const targetSizeKB = parseInt(sizeSlider.value);
// Hard Compression Logic: Quality + Scaling
const resultBlob = await hardCompress(sourceData, targetSizeKB, selectedFormat);
progressBar.style.width = '100%';
const finalSizeKB = (resultBlob.size / 1024).toFixed(1);
sizeInfo.innerText = `Original: ${(file.size/1024/1024).toFixed(1)}MB -> Final: ${finalSizeKB}KB`;
const url = URL.createObjectURL(resultBlob);
const name = `compressed_${Date.now()}.${selectedFormat.toLowerCase()}`;
downloadContainer.innerHTML = `
<a href="${url}" download="${name}" class="block w-full text-center bg-blue-600 hover:bg-blue-500 text-white font-bold py-3 rounded-lg shadow-xl uppercase tracking-widest text-sm">
Download Result
</a>
`;
document.getElementById('statusBadge').innerText = 'Ready';
document.getElementById('statusBadge').className = 'text-xs bg-green-500/20 text-green-400 px-2 py-1 rounded';
} catch (err) {
console.error(err);
document.getElementById('statusBadge').innerText = 'Failed';
sizeInfo.innerText = "Error: File too complex or invalid.";
}
};
async function fileToImageData(file) {
return new Promise((resolve) => {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => resolve(img);
img.src = e.target.result;
};
reader.readAsDataURL(file);
});
}
async function pdfToImageData(file) {
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument(arrayBuffer).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 2.0 });
const canvas = document.createElement('canvas');
canvas.height = viewport.height;
canvas.width = viewport.width;
await page.render({ canvasContext: canvas.getContext('2d'), viewport }).promise;
const img = new Image();
img.src = canvas.toDataURL();
return new Promise(resolve => img.onload = () => resolve(img));
}
async function hardCompress(img, targetKB, format) {
const mime = `image/${format === 'JPG' ? 'jpeg' : format.toLowerCase()}`;
let scale = 1.0;
let quality = 0.85;
let blob = null;
let attempts = 0;
// Step 1: Quality Binary Search (Up to 5 iterations)
while (attempts < 5) {
blob = await getBlob(img, scale, quality, mime, format);
if (blob.size / 1024 <= targetKB) break;
quality -= 0.15;
if (quality < 0.2) break;
attempts++;
}
// Step 2: Hard Scaling (If quality reduction wasn't enough)
// This is how we get a 3MB file under 500KB reliably
while (blob.size / 1024 > targetKB && scale > 0.1) {
scale -= 0.15;
blob = await getBlob(img, scale, quality, mime, format);
}
return blob;
}
function getBlob(img, scale, quality, mime, format) {
return new Promise(resolve => {
const canvas = document.createElement('canvas');
canvas.width = img.width * scale;
canvas.height = img.height * scale;
const ctx = canvas.getContext('2d');
if (format === 'JPG') {
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
if (format === 'PNG') {
canvas.toBlob(resolve, mime);
} else {
canvas.toBlob(resolve, mime, quality);
}
});
}
initWheel();
</script>
</body>
</html>