From f20b8ebd6693c349e42b94ae59d6f096222170e5 Mon Sep 17 00:00:00 2001 From: kura Date: Wed, 18 Dec 2024 10:36:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E9=80=89=E6=8B=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 10244 -> 10244 bytes test/index.html | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/.DS_Store b/.DS_Store index cf0ecf5ac09d8cb0bc3d3a251f47946553eb4e3c..48950926896c3ae5219be15e90017d5d72d30cef 100644 GIT binary patch delta 100 zcmZn(XbG6$F8U^hRb&So9~7iL~IhJ1!nh7yJX5IOmefc)eDVGCtWh9ZVkhD3%G zhFl=62P9KGbMljua`KZH7#IW?7#L+IuNPL`JWrULXEVFPFP6z%G9H^>i|l3s03+QR Aod5s; delta 45 zcmZn(XbG6$LAU^hRb)@B|77v{+|!WNTdM8r1#5$5KZSP-|FUEvqY=6F$NW&lx; B4le)z 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);