/** * nase64Data --> 要壓縮的圖片base64數據 * width --> 寬度 * height --> 高度 * _callback --> 回調函數 */ function getCompressImage(base64Data, width, height, _callback) { var oldimg = new Image(); oldimg.src = base64Data; var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); canvas.width = width; canvas.height = height; oldimg.onload = function() { console.log('原始寬:' + oldimg.width + "原始高:" + oldimg.height); // 繪製 context.drawImage(oldimg, 0, 0, width, height); // 新的base64數據(壓縮後的數據) var newBase64 = canvas.toDataURL(); return _callback(newBase64); } }