// <img id="img" src='https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1541397796419&di=91f86a71702405a5af928f4b7be79cc7&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201512%2F30%2F20151230162728_iPGVN.jpeg' />
var img = docment.getElementById("img");
var base64,blob,imgFIle;
// img to canvas
function imgToCanvas(imgElement){
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
canvas.getContext("2d").drawImage(image, 0, 0);
return canvas;
}
// canvas to blob
canvas.toBlob(function(blob) {
});
//canvas to base64
canvas.toDataURL();
// base64 to file
function 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 }); // file 类型
}
//1、FileReader 转化为 base64
var reader = new FileReader();
reader.onload = function (fileEvent) {
base64 = fileEvent.target.result;
}
reader.readAsDataURL(imgFile) // imgFile 必须是从 input 中选择出来的
//