compress.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  13. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. var __read = (this && this.__read) || function (o, n) {
  49. var m = typeof Symbol === "function" && o[Symbol.iterator];
  50. if (!m) return o;
  51. var i = m.call(o), r, ar = [], e;
  52. try {
  53. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  54. }
  55. catch (error) { e = { error: error }; }
  56. finally {
  57. try {
  58. if (r && !r.done && (m = i["return"])) m.call(i);
  59. }
  60. finally { if (e) throw e.error; }
  61. }
  62. return ar;
  63. };
  64. var __spread = (this && this.__spread) || function () {
  65. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  66. return ar;
  67. };
  68. import { EXIF } from 'exif-js';
  69. import { createObjectURL, getTransform } from './utils';
  70. var mimeTypes = {
  71. PNG: 'image/png',
  72. JPEG: 'image/jpeg',
  73. WEBP: 'image/webp',
  74. BMP: 'image/bmp'
  75. };
  76. var maxSteps = 4;
  77. var scaleFactor = Math.log(2);
  78. var supportMimeTypes = Object.keys(mimeTypes).map(function (type) { return mimeTypes[type]; });
  79. var defaultType = mimeTypes.JPEG;
  80. function isSupportedType(type) {
  81. return supportMimeTypes.includes(type);
  82. }
  83. var Compress = /** @class */ (function () {
  84. function Compress(file, config) {
  85. this.file = file;
  86. this.config = config;
  87. this.config = __assign({ quality: 0.92, noCompressIfLarger: false }, this.config);
  88. }
  89. Compress.prototype.process = function () {
  90. return __awaiter(this, void 0, void 0, function () {
  91. var srcDimension, originImage, canvas, scale, scaleCanvas, distBlob;
  92. return __generator(this, function (_a) {
  93. switch (_a.label) {
  94. case 0:
  95. this.outputType = this.file.type;
  96. srcDimension = {};
  97. if (!isSupportedType(this.file.type)) {
  98. throw new Error("unsupported file type: " + this.file.type);
  99. }
  100. return [4 /*yield*/, this.getOriginImage()];
  101. case 1:
  102. originImage = _a.sent();
  103. return [4 /*yield*/, this.getCanvas(originImage)];
  104. case 2:
  105. canvas = _a.sent();
  106. scale = 1;
  107. if (this.config.maxWidth) {
  108. scale = Math.min(1, this.config.maxWidth / canvas.width);
  109. }
  110. if (this.config.maxHeight) {
  111. scale = Math.min(1, scale, this.config.maxHeight / canvas.height);
  112. }
  113. srcDimension.width = canvas.width;
  114. srcDimension.height = canvas.height;
  115. return [4 /*yield*/, this.doScale(canvas, scale)];
  116. case 3:
  117. scaleCanvas = _a.sent();
  118. distBlob = this.toBlob(scaleCanvas);
  119. if (distBlob.size > this.file.size && this.config.noCompressIfLarger) {
  120. return [2 /*return*/, {
  121. dist: this.file,
  122. width: srcDimension.width,
  123. height: srcDimension.height
  124. }];
  125. }
  126. return [2 /*return*/, {
  127. dist: distBlob,
  128. width: scaleCanvas.width,
  129. height: scaleCanvas.height
  130. }];
  131. }
  132. });
  133. });
  134. };
  135. Compress.prototype.clear = function (ctx, width, height) {
  136. // jpeg 没有 alpha 通道,透明区间会被填充成黑色,这里把透明区间填充为白色
  137. if (this.outputType === defaultType) {
  138. ctx.fillStyle = '#fff';
  139. ctx.fillRect(0, 0, width, height);
  140. }
  141. else {
  142. ctx.clearRect(0, 0, width, height);
  143. }
  144. };
  145. /** 通过 file 初始化 image 对象 */
  146. Compress.prototype.getOriginImage = function () {
  147. var _this = this;
  148. return new Promise(function (resolve, reject) {
  149. var url = createObjectURL(_this.file);
  150. var img = new Image();
  151. img.onload = function () {
  152. resolve(img);
  153. };
  154. img.onerror = function () {
  155. reject('image load error');
  156. };
  157. img.src = url;
  158. });
  159. };
  160. Compress.prototype.getCanvas = function (img) {
  161. var _this = this;
  162. return new Promise(function (resolve, reject) {
  163. // 通过得到图片的信息来调整显示方向以正确显示图片,主要解决 ios 系统上的图片会有旋转的问题
  164. EXIF.getData(img, function () {
  165. var orientation = EXIF.getTag(img, 'Orientation') || 1;
  166. var _a = getTransform(img, orientation), width = _a.width, height = _a.height, matrix = _a.matrix;
  167. var canvas = document.createElement('canvas');
  168. var context = canvas.getContext('2d');
  169. canvas.width = width;
  170. canvas.height = height;
  171. if (!context) {
  172. reject(new Error('context is null'));
  173. return;
  174. }
  175. _this.clear(context, width, height);
  176. context.transform.apply(context, __spread(matrix));
  177. context.drawImage(img, 0, 0);
  178. resolve(canvas);
  179. });
  180. });
  181. };
  182. Compress.prototype.doScale = function (source, scale) {
  183. return __awaiter(this, void 0, void 0, function () {
  184. var sctx, steps, factor, mirror, mctx, width, height, originWidth, originHeight, src, context, i, dw, dh, canvas, data;
  185. return __generator(this, function (_a) {
  186. if (scale === 1) {
  187. return [2 /*return*/, source];
  188. }
  189. sctx = source.getContext('2d');
  190. steps = Math.min(maxSteps, Math.ceil((1 / scale) / scaleFactor));
  191. factor = Math.pow(scale, (1 / steps));
  192. mirror = document.createElement('canvas');
  193. mctx = mirror.getContext('2d');
  194. width = source.width, height = source.height;
  195. originWidth = width;
  196. originHeight = height;
  197. mirror.width = width;
  198. mirror.height = height;
  199. if (!mctx || !sctx) {
  200. throw new Error("mctx or sctx can't be null");
  201. }
  202. for (i = 0; i < steps; i++) {
  203. dw = width * factor | 0 // eslint-disable-line no-bitwise
  204. ;
  205. dh = height * factor | 0 // eslint-disable-line no-bitwise
  206. ;
  207. // 到最后一步的时候 dw, dh 用目标缩放尺寸,否则会出现最后尺寸偏小的情况
  208. if (i === steps - 1) {
  209. dw = originWidth * scale;
  210. dh = originHeight * scale;
  211. }
  212. if (i % 2 === 0) {
  213. src = source;
  214. context = mctx;
  215. }
  216. else {
  217. src = mirror;
  218. context = sctx;
  219. }
  220. // 每次画前都清空,避免图像重叠
  221. this.clear(context, width, height);
  222. context.drawImage(src, 0, 0, width, height, 0, 0, dw, dh);
  223. width = dw;
  224. height = dh;
  225. }
  226. canvas = src === source ? mirror : source;
  227. data = context.getImageData(0, 0, width, height);
  228. // resize
  229. canvas.width = width;
  230. canvas.height = height;
  231. // store image data
  232. context.putImageData(data, 0, 0);
  233. return [2 /*return*/, canvas];
  234. });
  235. });
  236. };
  237. /** 这里把 base64 字符串转为 blob 对象 */
  238. Compress.prototype.toBlob = function (result) {
  239. var dataURL = result.toDataURL(this.outputType, this.config.quality);
  240. var buffer = atob(dataURL.split(',')[1]).split('').map(function (char) { return char.charCodeAt(0); });
  241. var blob = new Blob([new Uint8Array(buffer)], { type: this.outputType });
  242. return blob;
  243. };
  244. return Compress;
  245. }());
  246. var compressImage = function (file, options) { return new Compress(file, options).process(); };
  247. export default compressImage;
  248. //# sourceMappingURL=compress.js.map