更新记录

1.0.1(2024-09-12) 下载此版本

修复H5报错Compressor is not defined 如果还是报错,可以尝试切换引入方式

import './compressor.min.js'
import Compressor from './compressor.min.js'

1.0.0(2024-09-11) 下载此版本

实现APP、小程序、H5图片压缩


平台兼容性

HbuilderX/cli最低兼容版本
3.1.0

uni-app

Vue2 Vue3
?
app-vue app-nvue app-android app-ios app-harmony
? ? ? ? ?
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
? ? ? ? ? ? ? ? ?
微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序 钉钉小程序 快手小程序 飞书小程序 京东小程序
? ? ? ? ? ? ? ? ?
快应用-华为 快应用-联盟
? ?

uni-app x

app-android app-ios
? ?
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
? ? ? ? ? ? ? ? ?

watasi-compress(图片压缩,支持APP、H5、小程序)

APP、小程序使用uni.compressImage做压缩,H5使用compressorjs

使用说明

属性 是否必填 值类型 默认值 说明
paths Array - 需要压缩的图片路径
options Object - 压缩参数

options

属性 值类型 说明
quality Number 压缩质量,默认0.8
width Number 压缩后图片的宽度,如果未指定,将使用原始图像的自然宽度
height Number 压缩后图片的高度,如果未指定,将使用原始图像的自然高度
success Function 压缩成功的回调,也可以使用.then接收
fail Function 压缩失败的回调,也可以使用.catch接收

示例

<template>
    <view class="content">
        <block v-for="(item, index) in images" :key="index">
            <view class="cell">
                <view class="item">
                    <image :src="item.before" mode="aspectFit" class="img"></image>
                    <text class="text">压缩前:{{ item.size1 }}</text>
                </view>
                <view class="item">
                    <image :src="item.after" mode="aspectFit" class="img"></image>
                    <text class="text">压缩后:{{ item.size2 }}</text>
                </view>
            </view>
        </block>

        <button @click="onChoiseImage">选择图片</button>
    </view>
</template>

<script>
    import WCompress from '@/uni_modules/watasi-compress/js_sdk/index.js'
    export default {
        data() {
            return {
                images: []
            }
        },
        onLoad() {

        },
        methods: {
            onChoiseImage() {
                uni.chooseImage({
                    count: 9,
                    success: (res) => {
                        this.images = res.tempFilePaths.map((c, i) => {
                            return {
                                before: c,
                                size1: parseInt(res.tempFiles[i].size / 1024) + 'KB',
                                after: '',
                                size2: 0,
                            }
                        })
                        WCompress(res.tempFilePaths, {
                            success: (imgs) => {
                                imgs.map((c, i) => {
                                    // #ifndef H5
                                    uni.getFileInfo({
                                        filePath: c,
                                        success: (e) => {
                                            this.images[i].after = c
                                            this.images[i].size2 = parseInt(e.size / 1024) + 'KB'
                                        }
                                    })
                                    // #endif

                                    // #ifdef H5
                                    this.images[i].after = URL.createObjectURL(c)
                                    this.images[i].size2 = parseInt(c.size / 1024) + 'KB'
                                    // #endif
                                })
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style lang="scss">
    .cell {
        display: flex;
        box-sizing: border-box;
        padding: 20rpx;
        .item {
            width: 50%;
            margin: 20rpx;
        }
        image, uni-image {
            max-width: 100%;
        }
    }
</style>

其他说明

  • 当传入widthheight时,如果图片原始尺寸小于传入的尺寸,可能导致压缩的图片变大

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT协议

暂无用户评论。

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问