-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.html
89 lines (73 loc) · 3.24 KB
/
example.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Image Encode</title>
<script type="module">
import { loadImage, encodeWebP,encodeWebP2, encodeAvif, encodeJpeg, encodeJxl, rotateImage, resizeImage, encodeOnixPng, quantizeImage, resizePixelImage } from "https://cdn.jsdelivr.net/gh/thangman22/frontend-image-encode@main/index.js";
function getImageSize(img) {
var base64str = img.split('base64,')[1];
var decoded = atob(base64str);
return bytesToSize(decoded.length)
}
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return (bytes / Math.pow(1024, i)).toFixed(3) + ' ' + sizes[i];
}
(async () => {
const image = await loadImage("./assets/images/example.jpg");
const imagePixel = await loadImage("./assets/images/example_pixel.png");
console.log('Image Loaded');
const webPImageResult = await encodeWebP(image);
document.getElementById('webp-image').src = webPImageResult
console.log(`Webp Encoded ${getImageSize(webPImageResult)}`);
const pngImageResult = await encodeOnixPng(image);
document.getElementById('png-image').src = pngImageResult
console.log(`OnixPng Encoded ${getImageSize(pngImageResult)}`);
const avifImageResult = await encodeAvif(image);
document.getElementById('avif-image').src = avifImageResult
console.log(`Avif Encoded ${getImageSize(avifImageResult)}`);
const jpegImageResult = await encodeJpeg(image);
document.getElementById('jpeg-image').src = jpegImageResult
console.log(`Jpeg Encoded ${getImageSize(jpegImageResult)}`);
const rotateImageResult = await rotateImage(image, 90);
document.getElementById('rotate-image').src = rotateImageResult
console.log('Rotate Image');
const resizeImageResult = await resizeImage(image, 300, null, null, true)
document.getElementById('resize-image').src = resizeImageResult
console.log('Resize Image');
const resizePixelImageResult = await resizePixelImage(imagePixel)
document.getElementById('resize-pixel-image').src = resizePixelImageResult
console.log('Resize Pixel Image');
const quantizeResult = await quantizeImage(image);
document.getElementById('quantize-image').src = quantizeResult
console.log(`Quantize Encoded ${getImageSize(quantizeResult)}`);
})();
</script>
</head>
<body>
<div>
Original Image
<div><img src="./assets/images/example.jpg" width="500" /></div>
WebP Image
<div><img id="webp-image" width="500" /></div>
Avif Image
<div><img id="avif-image" width="500" /></div>
Jpeg Image
<div><img id="jpeg-image" width="500" /></div>
Png Image
<div><img id="png-image" width="500" /></div>
Rotate Image
<div><img id="rotate-image" width="500" /></div>
Resize Pixel Image
<div><img id="resize-pixel-image" width="100" /></div>
Resize Image
<div><img id="resize-image" width="100" /></div>
Quantize Image
<div><img id="quantize-image" width="500" /></div>
</div>
</body>
</html>