upLoad(img, file,callback){
img.onload = ()=> {
var canvas = document.createElement('canvas')
var context = canvas.getContext('2d')
var originWidth = img.width
var originHeight = img.height
var maxWidth = 650, maxHeight = 650
var targetWidth = originWidth, targetHeight = originHeight;
if (originWidth > maxWidth || originHeight > maxHeight) {
if (originWidth / originHeight > maxWidth / maxHeight) {
targetWidth = maxWidth
targetHeight = Math.round(maxWidth * (originHeight / originWidth))
} else {
targetHeight = maxHeight
targetWidth = Math.round(maxHeight * (originWidth / originHeight))
}
}
canvas.width = targetWidth
canvas.height = targetHeight
context.clearRect(0, 0, targetWidth, targetHeight)
context.drawImage(img, 0, 0, targetWidth, targetHeight)
callback(canvas.toDataURL(file.file.type),1)
}
}
dataURLtoFile(dataurl, filename) {
var arr = dataurl.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 File([u8arr], filename, {type:mime});
}
afterRead(files){
let config = {
headers: {
"Content-Type": "multipart/form-data"
}
};
const that = this
const img = new Image()
img.src = files.content
this.upLoad(img, files, (val)=> {
let params = new FormData()
params.append("file", that.dataURLtoFile(val,files.file.name))
axios.post(this.base.uploadimg, params,config).then(res => {
if(res.data.code === 200){
this.card_img = res.data.data.img_url
this.$toast.success({
message: '上传图片成功',
duration: 400
})
this.disabled = false
}
}).catch(err => {
console.log(err)
})
})
}