-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
35 lines (31 loc) · 1.28 KB
/
script.js
File metadata and controls
35 lines (31 loc) · 1.28 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
function humanFileSize(size) {
const i = Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) + ' ' + ['B', 'KB', 'MB', 'GB'][i];
}
document.getElementById('imageInput').addEventListener('change', function (event) {
const file = event.target.files[0];
if (!file) return;
const fileInfo = document.getElementById('fileInfo');
fileInfo.innerHTML = `Original Size: ${humanFileSize(file.size)}`;
const quality = parseFloat(document.getElementById('quality').value);
new Compressor(file, {
quality: quality,
success(result) {
fileInfo.innerHTML += `<br>Compressed Size: ${humanFileSize(result.size)}`;
const downloadBtn = document.getElementById('downloadBtn');
const url = URL.createObjectURL(result);
downloadBtn.style.display = 'inline-block';
downloadBtn.onclick = () => {
const a = document.createElement('a');
a.href = url;
a.download = 'compressed-image.jpg';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
},
error(err) {
console.error(err.message);
},
});
});