lenye_base
Version:
基础方法
77 lines (63 loc) • 1.64 kB
JavaScript
import './get_tag.js';
import isString from './is_string.js';
/**
* Check image size
* @param {(Object|String)} image - image information,allow File Object or Data URLs
* @param {Object} [options={}] - Check options
*/
var DEFAULT = {
enabledMaxSize: false,
enabledNatural: false,
ratio: 1
};
var checkImageSize = function (image) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT,
enabledMaxSize = _ref.enabledMaxSize,
enabledNatural = _ref.enabledNatural,
ratio = _ref.ratio;
return new Promise((resolve, reject) => {
/**
* Check type of image
*/
if (image instanceof File) {
var reader = new FileReader();
reader.onload = () => {
checkSize(reader.result);
};
reader.readAsDataURL(image);
} else if (isString(image)) {
checkSize(image);
}
/**
* Check picture size
* @param {String} data:Data URL
*/
function checkSize(url) {
var image = new Image();
image.src = url;
image.onload = () => {
var w = image.width / ratio;
var h = image.height / ratio;
if (enabledMaxSize) {
var nw = Math.min(w, 750 / 2);
h = h * (nw / w);
w = nw;
}
if (enabledNatural) {
w = image.naturalWidth / ratio;
h = image.naturalHeight / ratio;
}
w = w >> 0;
h = h >> 0;
resolve({
width: w,
height: h
});
};
image.onerror = e => {
reject(e);
};
}
});
};
export default checkImageSize;