1. 程式人生 > >圖片路徑轉為base64

圖片路徑轉為base64

nbsp ack eat function att all ont 節點 width

function dealImage(path, callback){  
    var img = new Image();  
    img.src = path;  
    img.onload = function(){  
        //默認按比例壓縮  
        var w = this.width,  
            h = this.height; 
        var quality = 0.95; // 默認圖片質量為0.7  
          
        //生成canvas  
        var canvas = document.createElement(‘canvas‘);  
        
var ctx = canvas.getContext(‘2d‘); // 創建屬性節點 canvas.setAttribute("width", w); canvas.setAttribute("height", h); ctx.drawImage(this, 0, 0, w, h); // quality值越小,所繪制出的圖像越模糊 var base64 = canvas.toDataURL(‘image/jpeg‘, quality);
// 回調函數返回base64的值 callback(base64); }; };

圖片路徑轉為base64