var compressImage = {
compressByFile: function(file, callBack, autoRotate) {
var that = this;
var ready=new FileReader();
ready.readAsDataURL(file);
ready.onload=function(){
var _that = this;
var re = _that.result;
that.compressByPath(re, callBack, autoRotate);
}
},
compressByPath: function(path, callBack, autoRotate){
var that = this;
var _img = new Image();
var obj = {};
_img.src = path;
_img.onload = function(){
var _that = this;
// 默认按比例压缩
var w = _that.width,
h = _that.height,
scale = w / h;
w = obj.width || w;
h = obj.height || (w / scale);
//var quality = 0.7; // 默认图片质量为0.7
var quality = 0.5; // 默认图片质量为0.7
//生成canvas
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 创建属性节点
var anw = document.createAttribute("width");
anw.nodeValue = w;
var anh = document.createAttribute("height");
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(_that, 0, 0, w, h);
that._rotateImage(_that, canvas, function(cv) {
// 图像质量
if(obj.quality && obj.quality <= 1 && obj.quality > 0){
quality = obj.quality;
}
// quality值越小,所绘制出的图像越模糊
var base64 = cv.toDataURL('image/jpeg', quality );
// 回调函数返回base64的值
var _file = that._convertBase64UrlToBlob(base64);
var _return = {
'fileBase64' : base64,
'fileBlob' : _file
};
callBack(_return);
});
/* console.log(canvas);
// 图像质量
if(obj.quality && obj.quality <= 1 && obj.quality > 0){
quality = obj.quality;
}
// quality值越小,所绘制出的图像越模糊
var base64 = canvas.toDataURL('image/jpeg', quality );
// 回调函数返回base64的值
var _file = that._convertBase64UrlToBlob(base64);
var _return = {
'fileBase64' : base64,
'fileBlob' : _file
};
callBack(_return);*/
}
},
_rotateImageAction: function(img, direction,canvas){
//alert(img);
//最小与最大旋转方向,图片旋转4次后回到原方向
var min_step = 0;
var max_step = 3;
//var img = document.getElementById(pid);
if (img == null)return;
//img的高度和宽度不能在img元素隐藏后获取,否则会出错
var height = img.height;
var width = img.width;
//var step = img.getAttribute('step');
var step = 2;
if (step == null) {
step = min_step;
}
if (direction == 'right') {
step++;
//旋转到原位置,即超过最大值
step > max_step && (step = min_step);
} else {
step--;
step < min_step && (step = max_step);
}
//img.setAttribute('step', step);
/*var canvas = document.getElementById('pic_' + pid);
if (canvas == null) {
img.style.display = 'none';
canvas = document.createElement('canvas');
canvas.setAttribute('id', 'pic_' + pid);
img.parentNode.appendChild(canvas);
} */
//旋转角度以弧度值为参数
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
return canvas;
},
_rotateImage: function(_file, _canvas, callBack) {
var that = this;
//旋转
EXIF.getData(_file, function() {
var _that = this;
// alert(EXIF.pretty(this));
EXIF.getAllTags(_that);
//alert(EXIF.getTag(this, 'Orientation'));
var _orientation = EXIF.getTag(_that, 'Orientation');
if(typeof(_orientation) !== 'undefined' && _orientation != null && _orientation != "" && _orientation != 1){
switch(_orientation){
case 6://需要顺时针(向左)90度旋转
//alert('需要顺时针(向左)90度旋转');
_canvas = that._rotateImageAction(_file,'left',_canvas);
break;
case 8://需要逆时针(向右)90度旋转
//alert('需要顺时针(向右)90度旋转');
_canvas = that._rotateImageAction(_file,'right',_canvas);
break;
case 3://需要180度旋转
//alert('需要180度旋转');
_canvas = that._rotateImageAction(_file,'right',_canvas);//转两次
_canvas = that._rotateImageAction(_file,'right',_canvas);
break;
}
}
callBack(_canvas);
});
},
_convertBase64UrlToBlob: function(urlData){
var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
};