图片转换成BASE64

/**
 * 压缩图片
 * @param {Object} file { path: '', size: '' }
 * @param {Number} limitSize 压缩目标 MB
 */
export const imgCompress = {
	MB: 1024 * 1024,
	canvasId: 'imgCanvas',
	ctx: uni.createCanvasContext('imgCanvas'),
	// 获取可使用窗口宽度(提前使用uni.getSystemInfo获取windowWidth存在globalData)
	rpxToPx(number) {
		return number / 750 * getApp().globalData.systemInfo.windowWidth
	},
	// 获取文件信息
	getFileInfo(path) {
		return new Promise((resolve, reject) => {
			uni.getFileInfo({
				filePath: path,
				success: (res) => {
					console.log('File Size =>', `${res.size / this.MB}MB`)
					resolve(res.size)
				},
				fail: () => reject(null)
			})
		})
	},
	// 获取图片信息
	getImageInfo(path) {
		return new Promise((resolve, reject) => {
			uni.getImageInfo({
				src: path,
				success: (res) => resolve(res),
				fail: () => reject(null)
			})
		})
	},
	// 判断是否达到压缩目标
	getCompressImage(file, limitSize) {
		if (file.size > this.MB * limitSize) {
			return this.calcImaeg(file.path, limitSize);
		} else {
			return file.url
		}
	},
	// 递归
	async calcImaeg(url, limitSize) {
		const size = await this.getFileInfo(url)
		if (size > this.MB * limitSize) {
			const imageInfo = await this.getImageInfo(url)
			var maxSide = Math.max(imageInfo.width, imageInfo.height);
			var windowW = this.rpxToPx(750)
			var scale = 1
			if (maxSide > windowW) {
				scale = windowW / maxSide;
			}
			var imageW = Math.floor(imageInfo.width * scale)
			var imageH = Math.floor(imageInfo.height * scale)
			const newPath = await this.getCanvasImage(url, imageW, imageH)
			return this.calcImaeg(newPath, limitSize)
		} else {
			return url
		}
	},
	// 绘画
	getCanvasImage(imagePath, imageW, imageH) {
		return new Promise((resolve, reject) => {
			this.ctx.drawImage(imagePath, 0, 0, imageW, imageH)
			this.ctx.draw(false, () => {
				uni.canvasToTempFilePath({
					canvasId: this.canvasId,
					x: 0,
					y: 0,
					width: imageW,
					height: imageH,
					quality: 1,
					success: (res) => resolve(res.tempFilePath),
					fail: () => reject(imagePath)
				})
			})
		})
	}
}

//图片转换成base64
imgToBase64(data){
        return new Promise((resolve,reject)=>{
            pathToBase64(data).then(base64 => {
                resolve(base64)
             }).catch(error => {
                console.error(error)
                reject(error)
            })        
        })            
    },

//调用:

let promises = [];
    for (let i = 0; i < picLength; i++) {
        var img = imgCompress.getCompressImage(this.filePathsList[i],0.25);
        let promise = this.imgToBase64(img).then(base64=>{
            that.urlString += base64;
        })
        promises.push(promise);
    }
console.log(this.urlString);
// 在所有图片保存完成后执行下一步操作
Promise.all(promises).then(() => {
    //处理的内容
}).catch((err) => {
	console.log('err', err)
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值