图片压缩并上传

HTML

<div id="img">压缩大图</div>

<img src="../123.png"/>

JS

引用jquery.js

将下面的代码保存一个就是文件直接引用即可

(function() {
	var compression = function(array) {
		var _this = this;
		_this.array = array;
		_this.array.fidelity = _this.array.fidelity == undefined ? 1 : _this.array.fidelity;
		var listType = ["jpeg", "png"];
		if(listType.indexOf(_this.array.type) == -1 && _this.array.type != undefined) {
			_this.array.type = undefined;
		}
		_this.array.type = _this.array.type == undefined ? "jpeg" : _this.array.type.toLowerCase();
		_this.array.imgFile = _this.array.imgFile == undefined ? function(base64) {} : _this.array.imgFile;
		var domId = document.getElementById(_this.array.domId);
		domId.style.position = "relative";
		var input = document.createElement("input");
		input.type = "file";
		input.style.position = "absolute";
		input.style.width = "100%";
		input.style.height = "100%";
		input.style.left = 0;
		input.style.top = 0;
		input.style.opacity = 0;
		input.accept = "image/jpeg,image/jpg,image/png";
		domId.appendChild(input);
		input.onchange = function(e) {
			_this.inputChange(e);
		}
	}
	compression.prototype = {
		inputChange(event) {
			var _this = this;
			for(var i = 0; i < event.target.files.length; i++) {
				_this.compress(_this.getObjectURL(event.target.files[i]), "1000", "1000");
			}
			event.target.value = "";
		},
		getObjectURL: function(file) {
			var url = null;
			ImgName = file.name;
			if(window.createObjectURL != undefined) {
				url = window.createObjectURL(file);
			} else if(window.URL != undefined) {
				url = window.URL.createObjectURL(file);
			} else if(window.webkitURL != undefined) {
				url = window.webkitURL.createObjectURL(file);
			}
			return url;
		},
		compress: function(img, MaximgW, MaximgH) {
			var _this = this;
			var image = new Image();
			image.src = img;
			var imageWidth = "";
			var imageHeight = "";
			image.onload = function() {
				var canvas = document.createElement('canvas');
				if(image.width < MaximgW && image.height < MaximgH) {
					imageWidth = image.width;
					imageHeight = image.height;
				} else {
					if(image.width > image.height) {
						imageWidth = MaximgW;
						imageHeight = MaximgW * (image.height / image.width);
					} else if(image.width < image.height) {
						imageHeight = MaximgH;
						imageWidth = MaximgH * (image.width / image.height);
					} else {
						imageWidth = MaximgW;
						imageHeight = MaximgH;
					}
				}
				canvas.width = imageWidth;
				canvas.height = imageHeight;
				var con = canvas.getContext('2d');
				con.clearRect(0, 0, canvas.width, canvas.height);
				con.drawImage(image, 0, 0, imageWidth, imageHeight);
				var base64 = canvas.toDataURL('image/' + _this.array.type, _this.array.fidelity).substr(0);
				_this.array.imgFile(base64,ImgName);
			}
		},
	}
	window.compression = compression;
}())

在自己的js代码中加入

var dd = new compression({
		domId: "img", // 上传图片的Dom 目前只支持id;
		type: "jpg", // 压缩后保存图片的类型,目前支持 jpeg , png   参数:jpeg png
		fidelity: .5, // 压缩比例 (0,1]
		imgFile: function(base64,ImgName) {
			$('img').prop('src',base64);
		}
	})

上面就完成了图片压缩并直接在页面上展示需要上传的图片上传服务器的话就是把上面获取到的base64按照逗号分割直接传给后台如:只需要给后台传  base64.split(",")[1]  即可。。

希望能够帮助大家,本来上面的那个代码是想直接上传附件供大家下载,但是好像下载还需要积分,所以就直接贴了代码。

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值