Skip to content

Commit ec4de4a

Browse files
authored
Update script.js
1 parent 3ff7dc9 commit ec4de4a

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

script.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
function humanFileSize(size) {
2-
let i = Math.floor(Math.log(size) / Math.log(1024));
3-
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'KB', 'MB', 'GB'][i];
2+
const i = Math.floor(Math.log(size) / Math.log(1024));
3+
return (size / Math.pow(1024, i)).toFixed(2) + ' ' + ['B', 'KB', 'MB', 'GB'][i];
44
}
55

66
document.getElementById('imageInput').addEventListener('change', function (event) {
77
const file = event.target.files[0];
88
if (!file) return;
9-
10-
document.getElementById('fileInfo').innerHTML = `Original Size: ${humanFileSize(file.size)}`;
11-
9+
10+
const fileInfo = document.getElementById('fileInfo');
11+
fileInfo.innerHTML = `Original Size: ${humanFileSize(file.size)}`;
12+
const quality = parseFloat(document.getElementById('quality').value);
13+
1214
new Compressor(file, {
13-
quality: 0.6,
15+
quality: quality,
1416
success(result) {
15-
const compressedBlob = result;
16-
document.getElementById('fileInfo').innerHTML += `<br>Compressed Size: ${humanFileSize(compressedBlob.size)}`;
17+
fileInfo.innerHTML += `<br>Compressed Size: ${humanFileSize(result.size)}`;
1718

1819
const downloadBtn = document.getElementById('downloadBtn');
19-
const url = URL.createObjectURL(compressedBlob);
20+
const url = URL.createObjectURL(result);
2021
downloadBtn.style.display = 'inline-block';
21-
downloadBtn.onclick = function() {
22+
downloadBtn.onclick = () => {
2223
const a = document.createElement('a');
2324
a.href = url;
2425
a.download = 'compressed-image.jpg';

0 commit comments

Comments
 (0)