js图片img转base64数据流并压缩处理

js转化本地或者线上图片,转base64数据流并压缩处理。

es5-回调处理:

// 参数说明:图片地址(本地或者线上url)、处理后返回的base64内容、指定图片类型
function imgToBase64(url, callback, outputFormat) {
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var img = new Image;
	img.crossOrigin = 'Anonymous';
	img.onload = function() {
		var width = img.width;
		var height = img.height;
		
		// 压缩比例 -- 可以自己修改参数。500px宽度以下原尺寸,大于500px比例处理
		var bili = Math.round(width / 500) || 1;
		var rate = 1 / bili;
		canvas.width = width * rate;
		canvas.height = height * rate;
		ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);
		var dataURL = canvas.toDataURL(outputFormat || 'image/jpeg');
		// 去除标头 -- 传递给后台时一般去除头部
		// var reg = new RegExp('^data:image/[^;]+;base64,');
		// dataURL = dataURL.replace(reg, '');
		callback.call(this, dataURL);
		canvas = null;
	}
	img.src = url;
};

// 调用:
imgToBase64("https://img0.bdstatic.com/static/searchdetail/img/logo-2X_2dd9a28.png", function(res){
	console.log(res)
});

es6-Promise处理:


// 参数说明:图片地址(本地或者线上url)、处理后返回的base64内容、指定图片类型
function imgToBase64(url, outputFormat) {
	return new Promise((resolve, reject) => {
		var canvas = document.createElement('canvas');
		var ctx = canvas.getContext('2d');
		var img = new Image;
		img.crossOrigin = 'Anonymous';
		img.onload = () => {
			var width = img.width;
			var height = img.height;
			
			// 压缩比例 -- 可以自己修改参数。500px宽度以下原尺寸,大于500px比例处理
			var bili = Math.round(width / 500) || 1;
			var rate = 1 / bili;
			canvas.width = width * rate;
			canvas.height = height * rate;
			ctx.drawImage(img, 0, 0, width, height, 0, 0, width * rate, height * rate);
			var dataURL = canvas.toDataURL(outputFormat || 'image/jpeg');
			// 去除标头 -- 传递给后台时一般去除头部
			// var reg = new RegExp('^data:image/[^;]+;base64,');
			// dataURL = dataURL.replace(reg, '');
			canvas = null;
			resolve(dataURL);
		}
		img.src = url;
	})
};

// 调用:
imgToBase64("https://img0.bdstatic.com/static/searchdetail/img/logo-2X_2dd9a28.png").then(res => console.log(res))


备注: 如果是前端自己用,不需要过滤base64的头部字符。往后端传递base64数据流时,一般会删去头部标志。es5 或es6 大同小异,喜欢哪个用哪个

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 JavaScript 的 FileReader 和 Canvas API 实现图片压缩换为 base64 编码。 首先,使用 FileReader 将图片读取为 Blob 对象,然后使用 Canvas API 将图片压缩为指定的宽度和高度,最后将压缩后的图片换为 base64 编码。 以下是一个示例代码: ``` function compressAndConvertToBase64(file, maxWidth, maxHeight, callback) { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(event) { const img = new Image(); img.src = event.target.result; img.onload = function() { const canvas = document.createElement('canvas'); let width = img.width; let height = img.height; if (width > height) { if (width > maxWidth) { height *= maxWidth / width; width = maxWidth; } } else { if (height > maxHeight) { width *= maxHeight / height; height = maxHeight; } } canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, width, height); const compressedBase64 = canvas.toDataURL('image/jpeg', 0.7); callback(compressedBase64); } } } ``` 其中,参数 `file` 是要压缩图片文件,`maxWidth` 和 `maxHeight` 分别是压缩后的图片最大宽度和高度,`callback` 是压缩完成后的回调函数。压缩后的图片质量可以通过 `toDataURL` 方法的第二个参数来控制,值越小表示压缩后的图片质量越低。 使用示例: ``` const fileInput = document.querySelector('#file-input'); fileInput.addEventListener('change', function() { const file = fileInput.files[0]; compressAndConvertToBase64(file, 800, 600, function(base64) { console.log(base64); }); }); ``` 以上代码可以将选择的文件压缩为最大宽度为 800、最大高度为 600 的图片,并将压缩后的图片换为 base64 编码输出到控制台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值