js壓縮圖片后再將base64轉換為file文件親測可用


<button id="upload">上傳圖片</button>
<input type="file" name="input_file" id="input_file" style="height: 84px; width: 111px;" />

 

var filechooser = document.getElementById("input_file");//inupt type="file" 的id
// 用於壓縮圖片的canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext('2d');
//瓦片canvas
var tCanvas = document.createElement("canvas");
var tctx = tCanvas.getContext("2d");
var maxsize = 100 * 1024;

 

//upload 是按鈕的id
$("#upload").on("click", function() {
filechooser.click();
})
.on("touchstart", function() {
$(this).addClass("touch")
})
.on("touchend", function() {
$(this).removeClass("touch")
});

 

var suffix;
filechooser.onchange = function() {
if (!this.files.length) return;
var files = Array.prototype.slice.call(this.files);
var file = files[0];
suffix = file.name.substring(file.name.indexOf("\."));
var reader = new FileReader();
//獲取圖片大小
var size = file.size / 1024 > 1024 ? (~~(10 * file.size / 1024 / 1024)) / 10 + "MB" : ~~(file.size / 1024) + "KB";
reader.onload = function() {
var result = this.result;
console.log(result)
var img = new Image();
img.src = result;
//如果圖片大小小於100kb,則直接上傳
if (result.length <= maxsize) {
img = null;
upload(result, file.type, $(li));
return;
}
// 圖片加載完畢之后進行壓縮,然后上傳
if (img.complete) {
callback();
} else {
img.onload = callback;
}
function callback() {
var data = compress(img, file.type);
var str = dataURLtoFile(data, 'tmp'+suffix);
}
};
reader.readAsDataURL(file);
};

//使用canvas對大圖片進行壓縮
function compress(img, type) {
var initSize = img.src.length;//應該是圖片的整個大小像素或圖片大小
var width = img.width;//應該是像素
var height = img.height;//應該是像素
//如果圖片大於四百萬像素,計算壓縮比並將大小壓至400萬以下
var ratio;
if ((ratio = width * height / 4000000) > 1) {
ratio = Math.sqrt(ratio);
width /= ratio;
height /= ratio;
} else {
ratio = 1;
}
canvas.width = width;
canvas.height = height;
//鋪底色
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
//如果圖片像素大於100萬則使用瓦片繪制
var count;
if ((count = width * height / 1000000) > 1) {
count = ~~(Math.sqrt(count) + 1); //計算要分成多少塊瓦片
//計算每塊瓦片的寬和高
var nw = ~~(width / count);
var nh = ~~(height / count);
tCanvas.width = nw;
tCanvas.height = nh;
for (var i = 0; i < count; i++) {
for (var j = 0; j < count; j++) {
tctx.drawImage(img, i * nw * ratio, j * nh * ratio, nw * ratio, nh * ratio, 0, 0, nw, nh);
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh);
}
}
} else {
ctx.drawImage(img, 0, 0, width, height);
}
//進行最小壓縮
var ndata = canvas.toDataURL(type, 0.1);
console.log('壓縮前:' + initSize);
console.log('壓縮后:' + ndata.length);
console.log('壓縮率:' + ~~(100 * (initSize - ndata.length) / initSize) + "%");
tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0;
return ndata;
}

//將base64轉換為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});
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM