原文

1、jszip.js 压缩并保存图片
2、FileSaver.js 保存压缩文件

  1. /**
  2. * 将图片转换成base64 并返回路径
  3. * @param img
  4. * @param {number} width 调用时传入具体像素值,控制大小 ,不传则默认图像大小
  5. * @param {number} height
  6. * @returns {string}
  7. */
  8. function getBase64Image(img,width = 0,height = 0) {
  9. const canvas = document.createElement('canvas');
  10. canvas.width = width ? width : img.width;
  11. canvas.height = height ? height : img.height;
  12. const ctx = canvas.getContext('2d');
  13. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  14. const dataURL = canvas.toDataURL();
  15. return dataURL;
  16. }
  17. /**
  18. * 检查是不是Base64
  19. * @param img
  20. * @returns {boolean}
  21. */
  22. function IsBase64(img) {
  23. // jpg jpeg png gif
  24. const _img = img.toLowerCase();
  25. if (_img.endsWith('jpg') || _img.endsWith('jpeg') || _img.endsWith('png') || _img.endsWith('gif') ) return false;
  26. return true;
  27. }
  28. /**
  29. * 加载图片 加载成功后经图片返回
  30. * @param img
  31. * @returns {Promise<any>}
  32. */
  33. function getBase64(img){
  34. let url: string;
  35. if (IsBase64(img)) {
  36. // 有一些数据 后台没有返回前缀
  37. const _base64 = 'data:image/jpeg;base64,';
  38. if (img.startsWith(_base64)) {
  39. url = img;
  40. } else {
  41. url = _base64 + img;
  42. }
  43. return url;
  44. } else {
  45. url = img;
  46. const image = new Image();
  47. image.crossOrigin = '*';
  48. image.src = url;
  49. return new Promise(function (resolve, reject) {
  50. image.onload =function (){
  51. resolve(getBase64Image(image));//将base64传给done上传处理
  52. }
  53. });
  54. }
  55. }
  56. /**
  57. * 压缩图片
  58. */
  59. function setBase64Img(zip, imgFolder, base64, imgArr, index, downloadName) {
  60. base64 = base64.split('base64,')[1];
  61. imgFolder.file(downloadName + '_' + index + '.png', base64, { base64: true });
  62. if (index === imgArr.length - 1) {
  63. zip.generateAsync({ type: 'blob' }).then((blob) => {
  64. saveAs(blob, downloadName + '.zip');
  65. });
  66. }
  67. }
  68. /**
  69. * 下载压缩图片
  70. * @param {any[]} imgArr 图片合集
  71. * @param {string} imgKey 如果不是单纯的图片路径 需要传入路径的key
  72. */
  73. export function downloadZipImage (imgArr: any[], imgKey = '', downloadName = 'img') {
  74. if (!imgArr || !imgArr.length) {
  75. return;
  76. }
  77. const zip = new JSZip();
  78. // 创建images文件夹
  79. const imgFolder = zip.folder('images');
  80. // test
  81. // imgArr = ['assets/img/charts/return-upper-level.png',
  82. // 'assets/img/tree/arrow_right.png',
  83. // 'assets/img/tree/arrow_left.png',
  84. // 'assets/img/charts/map_ShadowMap.png'];
  85. let index = 0; // 判断加载了几张图片的标识
  86. for (let i = 0;i < imgArr.length;i++) {
  87. const itemImg = imgKey ? imgArr[i][imgKey] : imgArr[i];
  88. /**
  89. * 如果是Base64就不需要再做异步处理了
  90. */
  91. const Base64Img = getBase64(itemImg);
  92. if (Base64Img['then']) {
  93. Base64Img['then'](function(base64: string){
  94. setBase64Img(zip, imgFolder, base64, imgArr, index, downloadName);
  95. index++;
  96. },function(err){
  97. console.log(err);//打印异常信息
  98. });
  99. } else {
  100. setBase64Img(zip, imgFolder, Base64Img, imgArr, index, downloadName);
  101. index++;
  102. }
  103. }
  104. }