v.pics.forEach((u, i) => { if (u.split('/')[0] == 'cloud:') {} else { picCursor.push(i) tmpPromise.push(wx.compressImage({ src: u, quality: 30, }).then(res => { return wx.cloud.uploadFile({ cloudPath: 'usersproducts/' + upID + Date.now() + (i + '.png'), filePath: res.tempFilePath, }) })) } })这段代码中如何增加一个判断原始图片大小的功能,如果图片大于200k b就进行压缩,如果图片小于200k b就不压缩
时间: 2024-01-27 09:05:57 浏览: 33
可以通过获取图片文件大小来判断是否需要进行压缩,可以使用 wx.getFileInfo API 获取图片文件大小,代码如下:
```
v.pics.forEach((u, i) => {
if (u.split('/')[0] == 'cloud:') {
} else {
wx.getFileInfo({
filePath: u,
success: function (res) {
// 判断图片大小是否大于 200KB
if (res.size > 200 * 1024) {
picCursor.push(i)
tmpPromise.push(wx.compressImage({
src: u,
quality: 30,
}).then(res => {
return wx.cloud.uploadFile({
cloudPath: 'usersproducts/' + upID + Date.now() + (i + '.png'),
filePath: res.tempFilePath,
})
}))
} else {
// 不压缩,直接上传
picCursor.push(i)
tmpPromise.push(wx.cloud.uploadFile({
cloudPath: 'usersproducts/' + upID + Date.now() + (i + '.png'),
filePath: u,
}))
}
}
})
}
})
```
在获取图片文件大小后,判断大小是否超过 200KB,如果超过,则进行压缩,否则直接上传原图。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<script type="text/javascript"> var httpRequest = new XMLHttpRequest(); httpRequest.open('GET','http://1.14.137.211:8888/tealist',true); httpRequest.send(); httpRequest.onreadystatechange = function(){ if(httpRequest.readyState == 4 && httpRequest.status ==200 ){ var data = httpRequest.responseText; var data = JSON.parse(data); console.log(data); function test1(){ $(".increment").onclick = function(){ let n = $(this).siblings(".itxt").val(); console.log(n); n++; $(this).siblings(".itxt").val(n); } return; } function test2(){ $(".decrement").onclick = function(){ let n = $(this).siblings(".itxt").val(); console.log(n); if (n == 0) { return false; } n--; $(this).siblings(".itxt").val(n); } return; } data.forEach(element => { let html = ${element.name}${element.price}¥ - <input type="text" class="itxt" value="0"> + $("#pics").append(html); //document.querySelector("#pics").innerHTML = html; }); } } </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)