[点晴永久免费OA]【JavaScript】纯JS代码在线实时压缩图片
当前位置:点晴教程→点晴OA办公管理信息系统
→『 经验分享&问题答疑 』
1. // 获取 canvas 元素 2. var canvas = document.getElementById('canvas'); 3. var ctx = canvas.getContext('2d'); 4. 5. // 创建 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. // 大小单位:字节 7. return (str.length * 0.75).toFixed(2); 8. } 9. 10./** 11. * 图像压缩,默认同比例压缩 12. * @param {Object} imgPath 13. * 图像base64编码字符串或图像可访问路径 14. * @param {Object} obj 15. * obj 对象 有 width, height, quality(0-1) 16. * @param {Object} maxSize 17. * 指定压缩后的文件大小,单位:字节 18. * @param {Object} callback 19. * 回调函数有一个参数,base64的字符串数据 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. // 默认按比例压缩 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. // 图像质量,默认图片质量为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. // 回调函数返回压缩后的 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 编辑过 |
关键字查询
相关文章
正在查询... |