-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (111 loc) · 3.79 KB
/
script.js
File metadata and controls
139 lines (111 loc) · 3.79 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
let selectedFile = null;
let compressionLevel = 50;
const $ = id => document.getElementById(id);
// Events
$('fileInput').onchange = e => handleFile(e.target.files[0]);
['dragover', 'dragleave', 'drop'].forEach(event => {
$('uploadZone').addEventListener(event, e => {
e.preventDefault();
if (event === 'dragover') {
uploadZone.style.borderColor = '#3B82F6';
uploadZone.style.background = '#F0F7FF';
} else {
uploadZone.style.borderColor = '#D1D5DB';
uploadZone.style.background = '#FAFBFC';
}
if (event === 'drop') handleFile(e.dataTransfer.files[0]);
});
});
$('compressionSlider').oninput = e => {
compressionLevel = +e.target.value;
$('compressionValue').textContent = compressionLevel + '%';
$('compressionLabel').textContent =
compressionLevel < 30 ? 'Low Compression (High Quality)' :
compressionLevel < 70 ? 'Balanced (Good for Web)' :
'High Compression (Smallest Size)';
};
// Handle file
function handleFile(file) {
if (!file) return;
selectedFile = file;
$('uploadZone').classList.add('hidden');
$('fileSection').classList.remove('hidden');
$('resultsSection').classList.add('hidden');
$('fileName').textContent = file.name;
$('fileSize').textContent =
`${formatSize(file.size)} • ${file.type.split('/')[1]?.toUpperCase()}`;
$('fileIcon').textContent = file.type.startsWith('image/') ? '🖼️' : '📄';
}
// Compress
async function compressFile() {
if (!selectedFile) return;
const btn = $('compressBtn');
btn.disabled = true;
btn.innerHTML = 'Compressing...';
try {
const { blob, url } = await compressImage(selectedFile, compressionLevel / 100);
showResult(selectedFile.size, blob.size, url);
} catch {
alert('Compression failed');
}
btn.disabled = false;
btn.innerHTML = 'Compress File';
}
// Image compression
function compressImage(file, quality) {
return new Promise((res, rej) => {
const img = new Image();
img.src = URL.createObjectURL(file);
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let [w, h] = [img.width, img.height];
const max = 2400;
if (w > max || h > max) {
const ratio = w > h ? max / w : max / h;
w *= ratio;
h *= ratio;
}
canvas.width = w;
canvas.height = h;
ctx.drawImage(img, 0, 0, w, h);
canvas.toBlob(blob => {
if (!blob) return rej();
res({ blob, url: URL.createObjectURL(blob) });
}, file.type === 'image/png' ? 'image/jpeg' : file.type, Math.max(0.1, 1 - quality));
};
img.onerror = rej;
});
}
// Results + Caption
function showResult(orig, comp, url) {
$('resultsSection').classList.remove('hidden');
const saved = Math.round((1 - comp / orig) * 100);
$('originalSize').textContent = formatSize(orig);
$('newSize').textContent = formatSize(comp);
$('savedPercent').textContent = `-${saved}%`;
$('downloadSize').textContent = `(${formatSize(comp)})`;
const download = $('downloadBtn');
download.href = url;
download.download = 'compressed-' + selectedFile.name;
// Preview
$('previewCard').innerHTML = selectedFile.type.startsWith('image/') ?
`
<img src="${url}" class="preview-img">
` :
`<p>Preview not available</p>`;
}
// Clear Current file
function clearFile() {
selectedFile = null;
$('fileInput').value = '';
['uploadZone'].forEach(id => $(id).classList.remove('hidden'));
['fileSection', 'resultsSection'].forEach(id => $(id).classList.add('hidden'));
$('previewCard').innerHTML = '';
}
// Size formatter
function formatSize(b) {
if (!b) return '0 Bytes';
const i = Math.floor(Math.log(b) / Math.log(1024));
return (b / 1024 ** i).toFixed(2) + [' Bytes', ' KB', ' MB', ' GB'][i];
}