libwebp-compress/test/index.html

92 lines
107 KiB
HTML
Raw Normal View History

2024-11-06 03:40:07 +00:00
<body>
base64图
<img id="base64Image"
src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADb/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGQALQDAREAAhEBAxEB/8QAHwAAAAYDAQEBAAAAAAAAAAAAAwQFBgcIAQIJCgAL/8QAQRAAAgICAQMDBAECBQMCAgkFAQIDBAURBgcSIQATMQgUIkFRFTIJI0JhcRYkUoGhF5EKJT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Gvx0NkkEfGtgEeuFrMSLAbt839ww9oQag+ovjHUtyHMRqYy21SNu0Aab9Ht2PB18fvyP3v0gSQSS7l/YttjqMkdMQ0pUSXaxb0fp+p9Y20oOvJbR7R/Pco0deTvz486BI1/uoYBiHGGHyJFhm5hUpAclONRAfYDoY2RXGtABvOyNt538+dqdAAEjWj8nevRlJdLCxaz2v1AH6Q8PuwO+f2eNShHn8TslVI2Btj3DwPBP6G/58+hhsEMTu+PMXHof2hoIUDy3eBWQqCzMjHwQvgdvaBvtA1osATrXjf79IRcjIsX2NsMLMG9XLxytJAci9wxG1o0VNjuCnuB2QPj48eN6J18gnuIAHkAAIAxL7s1sAbAYIOD+5sPUXZywO1rDqG/mDCQ6HjY1pu0fHjZOzseR/wCpBHjZG/Sm5f0w30gmoEamNj6/XrGrsiBVUHySzaAAbzv589xJAH/O/wCNeltYjbnzhwLpbY36++fYt6QGig7AUrrZLHf9pBGxsHY/1fjoePB/lWzcYz0wW+mz4eOcOA48iWgYKNqqgMAPGyfIP7I+f/bX/B9NIBtcjPJ/nsfoTtD0kkgAhhe2G6HJd4EBkI2hj7T5/Pwd/B0O5fGx48H9+f0OBDBgCNs49xHFSnN9zsOflFb442WrZQ//AOU6O12itvYGtHwV0Se5dEgfrUxZIZnHV7b284qlrHeS1AWIAOwDcy1/0aDdFPuqVxNaEKKxA+T3EAlj5PaSPBO/Hj+dAmOEk+pbkxsfoesMmrZaFuwfALmwH6bcmzGtRCD+aKyA/l5IAAP/ALjY/j/n536jkvqcN4Qw6sTZ9xYc7XvDZkxJZjzDt82f76wsw1uxJVBY6k717R8q346P60Dtid78fsnXob2IbLX5f5gXeG1hYcwPPbP1xCYjvDc040A+hretkEN3DWj87AVgTsEnz6V
alt="Base64 Image">
<span>base64 图片大小:<span id="base64Size"></span></span>
WebP 图:
<img id="webpImage" alt="WebP Image">
<br>
<span>WebP 缩略图片大小:<span id="webpSize"></span></span>
<br>
</body>
<script>
function base64ToArrayBuffer(base64) {
var binaryString = atob(base64);
var len = binaryString.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// 返回 ArrayBuffer
return bytes.buffer;
}
// 将 WASM 生成的二进制数据转换成 Base64
function arrayBufferToBase64(buffer) {
let binary = '';
let len = buffer.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(buffer[i]);
}
return btoa(binary); // 使用 JavaScript 的 btoa() 函数
}
// 初始化 WebAssembly 模块
var Module = {
onRuntimeInitialized: function () {
// let baseData= document.getElementById("base64Image").src;
// baseData=baseData.split(",")[1];
// let buffer = base64ToArrayBuffer(baseData);
// decodeToWebpp(buffer);
fetch('./car.jpg').then(response => response.arrayBuffer()).then(buffer => {
decodeToWebpp(buffer);
})
}
};
function decodeToWebpp(buffer) {
// 显示原始图片大小
document.getElementById("base64Size").textContent = (buffer.byteLength / 1024).toFixed(2) + " KB";
// 转换图像为 Uint8Array
var inputData = new Uint8Array(buffer);
// 分配内存以存储输出 WebP 大小
var outputSizePtr = Module._malloc(4); // 存储输出大小的指针
// 设置目标宽高和质量因子
var targetWidth = 60; // 目标宽度
var targetHeight = 133; // 目标高度
var qualityFactor = 5 // 压缩质量 (0 - 100)
var inputDataPtr = Module._malloc(inputData.length);
Module.HEAPU8.set(inputData, inputDataPtr);
// 调用 WebAssembly 函数进行图像转换,返回 WebP 数据指针
var webpPtr = Module._convert_image_to_webp(inputDataPtr, inputData.length, targetWidth, targetHeight, qualityFactor, outputSizePtr);
// 获取 WebP 数据大小
var outputSize = Module.getValue(outputSizePtr, 'i32');
// 从 WebAssembly 内存中读取 WebP 数据
var webpData = new Uint8Array(Module.HEAPU8.buffer, webpPtr, outputSize);
// 显示 WebP 图片大小
document.getElementById("webpSize").textContent = (outputSize / 1024).toFixed(2) + " KB";
// 将 WebP 数据转换为 base64并显示
let img = document.getElementById("webpImage");
img.src = "data:image/webp;base64," + arrayBufferToBase64(webpData);
// 使用完成后释放内存
Module._free(outputSizePtr);
Module._free(webpPtr);
Module._free(inputDataPtr);
}
</script>
<script src="convert_image_to_webp.js"></script>