标签:rand his set name 图片处理 tab gettime mat height
//图片处理
afterimg(err, photos) {
var _this = this;
if(err && err != null && err != ‘‘) {
return ;
}
var src = ‘data:image/jpeg;base64,‘ + photos;
var inputData = {
fileName: ["upload", ‘_‘, new Date().getTime(), ‘_‘, Math.floor(Math.random() * 1000000), ‘.png‘].join(‘‘), // 文件全名,包括格式
fileContent: src
};
var width = parseInt(document.documentElement.clientWidth) * 0.8;
_this.dealImage(src, {
width: width
}, inputData, _this.uploadimage);
},
//压缩图片
dealImage(path, obj, data, cb) {
var img = new Image();
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.75; // 默认图片质量为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);
// 图像质量
if(obj.quality && obj.quality <= 1 && obj.quality > 0) {
quality = obj.quality;
}
var base64 = canvas.toDataURL(‘image/jpeg‘, quality).replace(‘data:image/jpeg;base64,‘, ‘‘);
// 回调函数返回base64的值
data.fileContent = base64;
typeof cb == ‘function‘ && cb(data);
}
},
//上传图片
uploadimage(inputData) {}
标签:rand his set name 图片处理 tab gettime mat height
原文地址:https://www.cnblogs.com/Super-scarlett/p/9224465.html