function compressImage(file, maxSize, minSize, callback) { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(event) { const img = new Image(); img.src = event.target.result; img.onload = function() { let canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); let width = img.width; let height = img.height; canvas.width = width; canvas.height = height; ctx.drawImage(img, 0, 0, width, height); let quality = 0.9; // Initial quality const step = 0.05; // Quality adjustment step const sizeStep = 0.9; // Dimension reduction step function compress() { canvas.toBlob(function(blob) { if (blob.size <= maxSize && blob.size >= minSize) { const reader
前端压缩图片至指定大小
最新推荐文章于 2024-06-14 11:01:38 发布