|
| 1 | +import { EXIF } from "exif-js"; |
| 2 | +import { createObjectURL, getTransform } from "./utils"; |
| 3 | + |
| 4 | +let mimeTypes = { |
| 5 | + PNG: "image/png", |
| 6 | + JPEG: "image/jpeg", |
| 7 | + WEBP: "image/webp", |
| 8 | + BMP: "image/bmp" |
| 9 | +}; |
| 10 | + |
| 11 | +let maxSteps = 4; |
| 12 | +let scaleFactor = Math.log(2); |
| 13 | +let supportMimeTypes = Object.keys(mimeTypes).map(type => mimeTypes[type]); |
| 14 | +let defaultType = mimeTypes.JPEG; |
| 15 | + |
| 16 | +function isSupportedType(type) { |
| 17 | + return supportMimeTypes.includes(type); |
| 18 | +} |
| 19 | + |
| 20 | +class Compress { |
| 21 | + constructor(file, option) { |
| 22 | + this.config = Object.assign( |
| 23 | + { |
| 24 | + quality:0.92, |
| 25 | + noCompressIfLarger:false |
| 26 | + }, |
| 27 | + option |
| 28 | + ); |
| 29 | + this.file = file; |
| 30 | + } |
| 31 | + |
| 32 | + process() { |
| 33 | + this.outputType = this.file.type; |
| 34 | + let srcDimension = {}; |
| 35 | + if (!isSupportedType(this.file.type)) { |
| 36 | + return Promise.reject(new Error(`unsupported file type: ${this.file.type}`)); |
| 37 | + } |
| 38 | + |
| 39 | + return this.getOriginImage() |
| 40 | + .then(img => { |
| 41 | + return this.getCanvas(img); |
| 42 | + }) |
| 43 | + .then(canvas => { |
| 44 | + // 计算图片缩小比例,取最小值,如果大于1则保持图片原尺寸 |
| 45 | + let scale = 1; |
| 46 | + if (this.config.maxWidth) { |
| 47 | + scale = Math.min(1, this.config.maxWidth / canvas.width); |
| 48 | + } |
| 49 | + if (this.config.maxHeight) { |
| 50 | + scale = Math.min(1, scale, this.config.maxHeight / canvas.height); |
| 51 | + } |
| 52 | + srcDimension.width = canvas.width; |
| 53 | + srcDimension.height = canvas.height; |
| 54 | + return this.doScale(canvas, scale); |
| 55 | + }) |
| 56 | + .then(result => { |
| 57 | + let distBlob = this.toBlob(result); |
| 58 | + if (distBlob.size > this.file.size && this.config.noCompressIfLarger){ |
| 59 | + return { |
| 60 | + dist: this.file, |
| 61 | + width: srcDimension.width, |
| 62 | + height: srcDimension.height |
| 63 | + }; |
| 64 | + } |
| 65 | + return ({ |
| 66 | + dist: distBlob, |
| 67 | + width: result.width, |
| 68 | + height: result.height |
| 69 | + }); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + clear(ctx, width, height) { |
| 74 | + // jpeg 没有 alpha 通道,透明区间会被填充成黑色,这里把透明区间填充为白色 |
| 75 | + if (this.outputType === defaultType) { |
| 76 | + ctx.fillStyle = "#fff"; |
| 77 | + ctx.fillRect(0, 0, width, height); |
| 78 | + } else { |
| 79 | + ctx.clearRect(0, 0, width, height); |
| 80 | + } |
| 81 | + } |
| 82 | + // 通过 file 初始化 image 对象 |
| 83 | + getOriginImage() { |
| 84 | + return new Promise((resolve, reject) => { |
| 85 | + let url = createObjectURL(this.file); |
| 86 | + let img = new Image(); |
| 87 | + img.onload = () => { |
| 88 | + resolve(img); |
| 89 | + }; |
| 90 | + img.onerror = () => { |
| 91 | + reject("image load error"); |
| 92 | + }; |
| 93 | + img.src = url; |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + getCanvas(img) { |
| 98 | + return new Promise((resolve, reject) => { |
| 99 | + // 通过得到图片的信息来调整显示方向以正确显示图片,主要解决 ios 系统上的图片会有旋转的问题 |
| 100 | + EXIF.getData(img, () => { |
| 101 | + let orientation = EXIF.getTag(img, "Orientation") || 1; |
| 102 | + let { width, height, matrix } = getTransform(img, orientation); |
| 103 | + let canvas = document.createElement("canvas"); |
| 104 | + let context = canvas.getContext("2d"); |
| 105 | + canvas.width = width; |
| 106 | + canvas.height = height; |
| 107 | + this.clear(context, width, height); |
| 108 | + context.transform(...matrix); |
| 109 | + context.drawImage(img, 0, 0); |
| 110 | + resolve(canvas); |
| 111 | + }); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + doScale(source, scale) { |
| 116 | + if (scale === 1) { |
| 117 | + return Promise.resolve(source); |
| 118 | + } |
| 119 | + // 不要一次性画图,通过设定的 step 次数,渐进式的画图,这样可以增加图片的清晰度,防止一次性画图导致的像素丢失严重 |
| 120 | + let sctx = source.getContext("2d"); |
| 121 | + let steps = Math.min(maxSteps, Math.ceil((1 / scale) / scaleFactor)); |
| 122 | + |
| 123 | + let factor = Math.pow(scale, 1 / steps); |
| 124 | + |
| 125 | + let mirror = document.createElement("canvas"); |
| 126 | + let mctx = mirror.getContext("2d"); |
| 127 | + |
| 128 | + let { width, height } = source; |
| 129 | + let originWidth = width; |
| 130 | + let originHeight = height; |
| 131 | + mirror.width = width; |
| 132 | + mirror.height = height; |
| 133 | + let src, context; |
| 134 | + |
| 135 | + for (let i = 0; i < steps; i++) { |
| 136 | + |
| 137 | + let dw = width * factor | 0; |
| 138 | + let dh = height * factor | 0; |
| 139 | + // 到最后一步的时候 dw, dh 用 目标缩放尺寸,否则会出现最后尺寸偏小的情况 |
| 140 | + if (i === steps - 1) { |
| 141 | + dw = originWidth * scale; |
| 142 | + dh = originHeight * scale; |
| 143 | + } |
| 144 | + |
| 145 | + if (i % 2 === 0) { |
| 146 | + src = source; |
| 147 | + context = mctx; |
| 148 | + } else { |
| 149 | + src = mirror; |
| 150 | + context = sctx; |
| 151 | + } |
| 152 | + // 每次画前都清空,避免图像重叠 |
| 153 | + this.clear(context, width, height); |
| 154 | + context.drawImage(src, 0, 0, width, height, 0, 0, dw, dh); |
| 155 | + width = dw; |
| 156 | + height = dh; |
| 157 | + } |
| 158 | + |
| 159 | + let canvas = src === source ? mirror : source; |
| 160 | + // save data |
| 161 | + let data = context.getImageData(0, 0, width, height); |
| 162 | + |
| 163 | + // resize |
| 164 | + canvas.width = width; |
| 165 | + canvas.height = height; |
| 166 | + |
| 167 | + // store image data |
| 168 | + context.putImageData(data, 0, 0); |
| 169 | + |
| 170 | + return Promise.resolve(canvas); |
| 171 | + } |
| 172 | + |
| 173 | + // 这里把 base64 字符串转为 blob 对象 |
| 174 | + toBlob(result) { |
| 175 | + let dataURL = result.toDataURL(this.outputType, this.config.quality); |
| 176 | + let buffer = atob(dataURL.split(",")[1]).split("").map(char => char.charCodeAt(0)); |
| 177 | + let blob = new Blob([new Uint8Array(buffer)], { type: this.outputType }); |
| 178 | + return blob; |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +let compressImage = (file, options) => { |
| 183 | + return new Compress(file, options).process(); |
| 184 | +}; |
| 185 | + |
| 186 | +export default compressImage; |
0 commit comments