[點(diǎn)晴永久免費(fèi)OA]【JavaScript】純JS代碼在線實(shí)時壓縮圖片
1. // 獲取 canvas 元素 2. var canvas = document.getElementById('canvas'); 3. var ctx = canvas.getContext('2d'); 4. 5. // 創(chuàng)建 Image 對象 6. var img = new Image(); 7. img.src = 'image.jpg'; 8. img.onload = function() { 9. // 繪制圖像到 canvas 10. canvas.width = img.width; 11. canvas.height = img.height; 12. ctx.drawImage(img, 0, 0, img.width, img.height); 13. 14. // 使用 toDataURL 方法壓縮圖像 15. var dataUrl = canvas.toDataURL('image/jpeg', 0.5); 16. 17. // 使用新的 Data URL 更新圖像 18. img.src = dataUrl; 19.} 1. var maxSize = 100 * 1024; // 最大文件大小為100KB 2. var img = new Image(); 3. img.src = base64Image; 4. img.onload = function () { 5. var canvas = document.createElement('canvas'); 6. var ctx = canvas.getContext('2d'); 7. var width = img.width; 8. var height = img.height; 9. canvas.width = width; 10. canvas.height = height; 11. ctx.drawImage(img, 0, 0, width, height); 12. var quality = 0.8; 13. var resultBlob; 14. do { 15. canvas.toBlob(function (blob) { 16. resultBlob = blob; 17. }, 'image/jpeg', quality); 18. quality -= 0.1; 19. } while (resultBlob.size > maxSize && quality > 0.1); 20. // do something with resultBlob, like create a new image from it or save it. 21.} 1. // 計算base64編碼圖片大小 2. function getBase64ImageSize(base64) { 3. const indexBase64 = base64.indexOf('base64,'); 4. if (indexBase64 < 0) return -1; 5. const str = base64.substr(indexBase64 + 6); 6. // 大小單位:字節(jié) 7. return (str.length * 0.75).toFixed(2); 8. } 9. 10./** 11. * 圖像壓縮,默認(rèn)同比例壓縮 12. * @param {Object} imgPath 13. * 圖像base64編碼字符串或圖像可訪問路徑 14. * @param {Object} obj 15. * obj 對象 有 width, height, quality(0-1) 16. * @param {Object} maxSize 17. * 指定壓縮后的文件大小,單位:字節(jié) 18. * @param {Object} callback 19. * 回調(diào)函數(shù)有一個參數(shù),base64的字符串?dāng)?shù)據(jù) 20. */ 21.function compressedImage(path, obj, maxSize, callback) { 22. let img = new Image(); 23. img.src = imgPath; 24. img.onload = function () { 25. const that = this; 26. // 默認(rèn)按比例壓縮 27. let w = that.width, 28. h = that.height, 29. scale = w / h; 30. w = obj.width || w; 31. h = obj.height && obj.height * (w / scale) || h; 32. // 生成canvas 33. let canvas = document.createElement('canvas'); 34. let ctx = canvas.getContext('2d'); 35. 36. canvas.width = w; 37. canvas.height = h; 38. 39. ctx.drawImage(that, 0, 0, w, h); 40. // 圖像質(zhì)量,默認(rèn)圖片質(zhì)量為0.8 41. let quality = 0.8; 42. if (obj.quality && obj.quality > 0 && obj.quality <= 1) { 43. quality = obj.quality; 44. } 45. // quality值越小,所繪制出的圖像越模糊 46. let newBase64Image = canvas.toDataURL('image/jpeg', quality); 47. 48. let fileSize = getBase64ImageSize(newBase64Image); 49. if (fileSize > maxSize && quality > 0.01) { 50. if (quality > 0.05) { 51. quality = quality - 0.05; 52. } else { 53. quality = 0.01; 54. } 55. compressedImage(imgPath, { 56. quality: quality 57. }, maxSize, callback); 58. return; 59. } 60. 61. // 回調(diào)函數(shù)返回壓縮后的 base64圖像 62. callback(newBase64Image); 63. } 64.} 1. // 在主線程中 2. var worker = new Worker('worker.js'); 3. worker.onmessage = function(e) { 4. var compressedImage = e.data; 5. // do something with compressedImage 6. }; 7. worker.postMessage({ 8. image: base64Image, 9. maxSize: 100 * 1024 // 最大文件大小為100KB 10.}); 1. // worker.js 2. self.onmessage = function(e) { 3. var image = e.data.image; 4. var maxSize = e.data.maxSize; 5. var img = new Image(); 6. img.src = image; 7. img.onload = function() { 8. var canvas = document.createElement('canvas'); 9. var ctx = canvas.getContext('2d'); 10. var width = img.width; 11. var height = img.height; 12. canvas.width = width; 13. canvas.height = height; 14. ctx.drawImage(img, 0, 0, width, height); 15. var quality = 0.8; 16. var resultBlob; 17. do { 18. canvas.toBlob(function(blob) { 19. resultBlob = blob; 20. self.postMessage(resultBlob); 21. }, 'image/jpeg', quality); 22. quality -= 0.1; 23. } while (resultBlob.size > maxSize && quality > 0.1); 24. } 25.}; 該文章在 2023/5/6 12:15:28 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |