//将图片压缩转成base64
function getBase64Image(img) {
//绘制图形
var canvas = document.createElement("canvas");
var width = img.width;
console.log(width);
var height = img.height;
console.log(height);
if(width > height) {
if(width > 300) {
height = Math.round(height = 300 / width);
width = 300;
}
} else {
if(height > 400) {
width = Math.round(width *= 400 / height);
height = 400;
}
}
canvas.width = width; /设置新的图片的宽度*/
canvas.height = height; /*设置新的图片的长度*/
var ctx = canvas.getContext("2d");
canvas.drawImage(img, 0, 0, width, height); /*绘图*/
var dataURL = canvas.toDataURL("image/png", 0.5);
console.log(dataURL);
return dataURL.replace("data:image/png;base64,", "");
}
var dataURL = canvas.toDataURL("image/png", 0.5);的时候抛出//Uncaught Error: SECURITY_ERR: DOM Exception 请问该怎么解决
Uncaught Error: SECURITY_ERR: DOM Exception 18
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-