更新记录
1.0.0(2026-10-11)
下载此版本
- 首次发布。
- 新增
compressImage 图片压缩接口,支持质量压缩与等比尺寸缩放。
- 支持微信/支付宝/百度/抖音/QQ/快手等小程序、H5、App(vue)。
- 小程序基于 Canvas 2D,H5/App 基于 HTML5 Canvas。
- 提供 easycom 组件
<hy-image-compress-js>,跨端统一调用。
- 参数与错误码与 UTS 版
hy-image-compress 保持一致。
平台兼容性
uni-app(5.05)
| Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
| √ |
√ |
√ |
√ |
√ |
× |
√ |
√ |
- |
| 微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
小红书小程序 |
快应用-华为 |
快应用-联盟 |
| √ |
√ |
√ |
√ |
√ |
√ |
- |
√ |
√ |
- |
- |
- |
hy-image-compress-js 图片压缩(JS 版)
纯 JS 跨端图片压缩插件,基于 Canvas 实现质量压缩与尺寸缩放,一套用法适配 小程序 / H5 / App(vue) 等常用平台。
与原生 UTS 插件 hy-image-compress(Android / iOS / Web / Harmony)保持一致的参数与返回结构,错误码也完全一致,便于两端无缝切换。
平台支持
| 平台 |
支持 |
实现方式 |
| 微信 / 支付宝 / 百度 / 抖音 / QQ / 快手等小程序 |
√ |
Canvas 2D (type="2d") + canvasToTempFilePath |
| H5 |
√ |
HTML5 Canvas + toBlob |
| App(vue) |
√ |
HTML5 Canvas(webview 内)+ 本地文件 |
| App(nvue) |
x |
nvue 不支持 document/canvas |
目录结构
uni_modules/hy-image-compress-js/
├── components/
│ └── hy-image-compress-js/
│ └── hy-image-compress-js.vue # 跨端组件(小程序端内置隐藏 canvas)
├── js_sdk/
│ └── hy-image-compress/
│ ├── index.js # 统一入口 compressImage
│ ├── html-canvas.js # H5 / App 实现
│ ├── mp-canvas.js # 小程序实现
│ └── utils.js # 公共工具
├── package.json
├── readme.md
└── changelog.md
使用方式
方式一:组件(推荐,全平台统一)
组件已内置 easycom,无需手动 import,直接使用标签 <hy-image-compress-js>。
<template>
<view>
<hy-image-compress-js ref="compressor" />
<button @click="doCompress">压缩图片</button>
</view>
</template>
<script>
export default {
methods: {
doCompress() {
uni.chooseImage({
count: 1,
success: (chooseRes) => {
const src = chooseRes.tempFilePaths[0]
this.$refs.compressor.compress({
src: src,
quality: 80,
maxWidth: 1280,
maxHeight: 1280,
format: 'jpg',
success: (res) => {
console.log('压缩成功', res.tempFilePath, res.size)
},
fail: (err) => {
console.error('压缩失败', err.errCode, err.errMsg)
}
})
}
})
}
}
}
</script>
方式二:函数(仅 H5 / App)
H5 / App 端无需 canvas 组件,可直接引入函数调用。
import { compressImage } from '@/uni_modules/hy-image-compress-js/js_sdk/hy-image-compress/index.js'
compressImage({
src: tempFilePath,
quality: 80,
maxWidth: 1280,
success(res) {},
fail(err) {},
complete(res) {}
})
小程序端调用函数必须传入 componentInstance(组件内)或 canvasNode,否则无法定位 canvas。因此小程序端请优先使用方式一组件。
API
compressImage(options) / this.$refs.compressor.compress(options)
| 参数 |
类型 |
必填 |
说明 |
| src |
string |
是 |
待压缩图片路径。小程序为本地/临时路径;H5 支持网络地址、blob/objectURL、base64 dataURL;App 支持本地路径 |
| quality |
number |
否 |
压缩质量 0-100,默认 80,仅对 jpg 生效 |
| maxWidth |
number |
否 |
压缩后最大宽度(px),≤0 或不传表示不限制 |
| maxHeight |
number |
否 |
压缩后最大高度(px),≤0 或不传表示不限制 |
| format |
string |
否 |
输出格式,'jpg'(默认)或 'png' |
| success |
function |
否 |
成功回调 |
| fail |
function |
否 |
失败回调 |
| complete |
function |
否 |
结束回调(成功、失败均执行) |
| componentInstance |
object |
否 |
小程序端组件实例(自定义组件内调用必传,组件方式已自动处理) |
| canvasNode |
object |
否 |
小程序端 canvas 节点(可选,传入后不再查询) |
成功回调 res
| 字段 |
类型 |
说明 |
| tempFilePath |
string |
压缩后图片路径(小程序为临时文件路径,H5/App 为 objectURL) |
| size |
number |
压缩后文件大小(字节) |
| width |
number |
压缩后宽度(px) |
| height |
number |
压缩后高度(px) |
| originalSize |
number |
压缩前文件大小(字节,尽力获取,失败为 0) |
| originalWidth |
number |
原始宽度(px) |
| originalHeight |
number |
原始高度(px) |
错误码
| errCode |
说明 |
| 9010001 |
参数错误 |
| 9010002 |
图片读取失败 |
| 9010003 |
图片解码失败 |
| 9010004 |
图片编码或保存失败 |
| 9010005 |
当前平台不支持该能力 |
缩放规则
- 等比缩放,只缩小不放大;
- 同时设置
maxWidth、maxHeight 时,取能满足两个限制的最小比例;
- 宽高均在限制值以内时保持原尺寸,仅做质量压缩。
注意事项
quality 仅对 jpg 生效,png 为无损压缩,忽略该参数。
- 小程序端使用 Canvas 2D 接口,要求小程序基础库版本支持
type="2d"(微信基础库 2.9.0+)。
- 小程序端压缩网络图片需配置 downloadFile 合法域名。
- H5 端压缩跨域图片时,服务器需返回 CORS 头,否则 canvas 会被污染导致导出失败。
- H5 / App 端返回的
tempFilePath 为 blob: 开头的 objectURL,如需上传请使用对应 blob 或及时转存。
- nvue 页面不支持,请使用 vue 页面。
更新日志
见 changelog.md