更新记录

1.0.0(2025-06-07) 下载此版本

首次发布


平台兼容性

uni-app(4.51)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 快应用-华为 快应用-联盟

uni-app x(4.51)

Chrome Safari Android iOS 鸿蒙 微信小程序

其他

多语言 暗黑模式 宽屏模式
× ×

hy-base64

hy-base64 是一个可以将本地图片转换为base64格式的插件,支持在App、H5、小程序中使用。

1.属性说明

属性名 类型 说明
toBase64() api 接口1:正常转换base64
tominBase64() api 接口2:压缩后转换base64

toBase64(path)

toBase64(path).then((res)=>{
    console.log(res);//res为转换后的base64
}).catch((err)=>{
    console.log(err);
})

tominBase64(path,ratio)

该接口不支持H5,H5只能使用 toBase64()

tominBase64(path,ratio).then((res)=>{
    //path为要转换的图片地址,ratio为图片压缩系数(0~100之间,越小压缩后图片尺寸越小),不输入该系数则自动压缩。
    console.log(res);//res为转换后的base64
}).catch((err)=>{
    console.log(err);
})

2.使用示例

页面:

<template>
    <view>
        <view style="display: flex;justify-content: space-between;width: 100vw;padding-top: 5px;">
            <view style="width: 100%;">
                <image @click="preimg(img1)" style="width: 100%;border: 1px solid #e0e0e0;box-sizing: border-box;" mode="aspectFit" :src="img1"></image>
                <view style="width: 100%;text-align: center;">本地图片</view>
            </view>     
            <view style="width: 100%;">
                <image @click="preimg(img2)" style="width: 100%;border: 1px solid #e0e0e0;box-sizing: border-box;" mode="aspectFit" :src="img2"></image>
                <view style="width: 100%;text-align: center;">base64</view>
            </view>         
        </view>

        <button @click="getimg">上传img</button>

        <view style="padding: 10px;box-sizing: border-box;font-size: 14px;">
            <view>本地图片</view>
            <pre style="white-space: pre-wrap;word-break: break-all;font-size: 10px;">{{img1}}</pre>
            <view>base64</view>
            <pre style="white-space: pre-wrap;word-break: break-all;font-size: 10px;">{{img2}}</pre>
        </view>
    </view>
</template>

script:

import {toBase64,tominBase64} from '../../js_sdk/hy-base64/index.js'
export default {
    data() {
        return {
            img1:'',//本地图片
            img2:''//base64
        }
    },
    methods: {
        getimg(){
            uni.chooseImage({
                count:1,
                success: (res) => {
                    console.log(res);
                    this.img1=res.tempFilePaths[0];
                    toBase64(this.img1).then((data)=>{
                        console.log(data);
                        this.img2=data;
                    }).catch((err)=>{
                        console.log(err);
                    })                      
                }
            })
        },
        preimg(e){
            uni.previewImage({
                urls:[e]
            })
        },
    }
}

说明

该插件使用时传入图片尺寸尽量小于3MB,如图片太大可能转换时间较长,转换的base64格式会比原文件更大。

隐私、权限声明

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

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

插件不采集任何数据

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

许可协议

MIT协议

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