-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
72 lines (57 loc) · 2.77 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Compressor</title>
</head>
<body>
<h1>Image Compressor</h1>
<p>Compresses your image file and turns it into a JPG!</p>
<div>
<p>Original Image Size: <span id="original-size">0</span> bytes</p>
<p>Compressed Image Size: <span id="compressed-size">0</span> bytes</p>
</div>
<input type="file" id="image-input" accept="image/*">
<button id="process-btn">Compress Image</button>
<a id="download-btn" style="display: none;">Download Compressed Image</a>
<script type="module">
import init, { process_image, get_compressed_size } from './pkg/compressor.js';
init().then(wasm => {
document.getElementById("image-input").addEventListener("change", (event) => {
const file = event.target.files[0];
if (file) {
document.getElementById("original-size").innerText = file.size;
} else {
document.getElementById("original-size").innerText = 0;
}
});
document.getElementById("process-btn").addEventListener("click", async () => {
const fileInput = document.getElementById('image-input');
const file = fileInput.files[0];
if (file) {
const arrayBuffer = await file.arrayBuffer();
const byteArray = new Uint8Array(arrayBuffer);
const compressedPtr = process_image(byteArray);
const compressedSize = get_compressed_size(byteArray);
if (compressedPtr === 0 || compressedSize === 0) {
alert("Compression failed or returned empty data.");
return;
}
const compressedData = new Uint8Array(wasm.memory.buffer, compressedPtr, compressedSize);
const blob = new Blob([compressedData], { type: "image/jpeg" });
const url = URL.createObjectURL(blob);
const downloadButton = document.getElementById("download-btn");
downloadButton.href = url;
downloadButton.download = "compressed_image.jpg";
downloadButton.innerText = "Download Compressed Image";
downloadButton.style.display = "inline";
document.getElementById("compressed-size").innerText = compressedSize;
} else {
alert("Please select an image file.");
}
});
}).catch(console.error);
</script>
</body>
</html>