Created
September 6, 2018 13:27
-
-
Save jrainlau/8a007e00dbdac2069ba944b713a31b21 to your computer and use it in GitHub Desktop.
choose and compress image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toPreviewer (dataUrl, cb) { | |
cb && cb(dataUrl) | |
} | |
function compress (img, fileType, maxWidth) { | |
let canvas = document.createElement('canvas') | |
let ctx = canvas.getContext('2d') | |
const proportion = img.width / img.height | |
const width = maxWidth | |
const height = maxWidth / proportion | |
canvas.width = width | |
canvas.height = height | |
ctx.fillStyle = '#fff' | |
ctx.fillRect(0, 0, canvas.width, canvas.height) | |
ctx.drawImage(img, 0, 0, width, height) | |
// 压缩 | |
const base64data = canvas.toDataURL(fileType, 0.75) | |
canvas = ctx = null | |
return base64data | |
} | |
function chooseImg (e, cb, maxsize = 200 * 1024) { | |
const file = e.target.files[0] | |
if (!file) { | |
return | |
} | |
if (!/\/(?:jpeg|jpg|png)/i.test(file.type)) { | |
return | |
} | |
const reader = new FileReader() | |
reader.onload = function () { | |
const result = this.result | |
let img = new Image() | |
// 如果图片小于 200kb,不压缩 | |
if (result.length <= maxsize) { | |
toPreviewer(result, cb) | |
return | |
} | |
img.onload = function () { | |
const compressedDataUrl = compress(img, file.type, maxsize / 1024) | |
toPreviewer(compressedDataUrl, cb) | |
img = null | |
} | |
img.src = result | |
} | |
reader.readAsDataURL(file) | |
} | |
export default chooseImg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment