diff --git a/.DS_Store b/.DS_Store index cf0ecf5..4895092 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/test/index.html b/test/index.html index e5c7c5a..d931c88 100644 --- a/test/index.html +++ b/test/index.html @@ -9,7 +9,17 @@ WebP Image

- + +
+ + +
+ + +
+ + +
@@ -67,8 +77,29 @@ }) } + function handleFileUpload(event) { + const file = event.target.files[0]; + if (file) { + const reader = new FileReader(); + reader.onload = function(e) { + const buffer = base64ToArrayBuffer(e.target.result.split(",")[1]); + const targetWidth = document.getElementById("width").value; + const targetHeight = document.getElementById("height").value; + const qualityFactor = document.getElementById("quality").value; + decodeToWebpp(buffer,targetWidth,targetHeight,qualityFactor); + }; + reader.readAsDataURL(file); + } + } - function decodeToWebpp(buffer) { + /** + * 将图片转换为 WebP 格式 + * @param {ArrayBuffer} buffer - 图片的二进制数据 + * @param {number} targetWidth - 目标宽度 + * @param {number} targetHeight - 目标高度 + * @param {number} qualityFactor - 压缩质量 (0 - 100) + */ + function decodeToWebpp(buffer,targetWidth=0,targetHeight=0,qualityFactor=50) { // 显示原始图片大小 let size = (buffer.byteLength / 1024).toFixed(2); @@ -79,9 +110,6 @@ var outputSizePtr = Module._malloc(4); // 存储输出大小的指针 // 设置目标宽高和质量因子 - var targetWidth = 300; // 目标宽度 - var targetHeight = 300; // 目标高度 - var qualityFactor = 50 // 压缩质量 (0 - 100) var inputDataPtr = Module._malloc(inputData.length); Module.HEAPU8.set(inputData, inputDataPtr); var webpPtr = Module._convert_image_to_webp(inputDataPtr, inputData.length, targetWidth, targetHeight, qualityFactor, outputSizePtr);