z-react-ui
Version:
z-react-ui,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。
69 lines (56 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
/**
* @description 限制图片的宽高
* @author ronffy
* @param {file: RcFile, option: CheckImageFileWHOption}
* @return {Promise<void>}
*/
function checkImageFileWH(file, _ref) {
var width = _ref.width,
height = _ref.height,
maxHeight = _ref.maxHeight,
maxWidth = _ref.maxWidth,
_ref$deviation = _ref.deviation,
deviation = _ref$deviation === void 0 ? 2 : _ref$deviation;
return new Promise(function (resolve, reject) {
if (!width && !height && !maxHeight && !maxWidth) {
resolve();
return;
}
var filereader = new FileReader();
filereader.onload = function (e) {
var src = e.target.result;
var image = new Image();
image.onload = function () {
var imgWidth = image.width;
var imgHeight = image.height;
if (Math.abs(width - imgWidth) > deviation) {
reject('width');
return;
}
if (Math.abs(height - imgHeight) > deviation) {
reject('height');
return;
}
if (maxWidth + deviation < imgWidth) {
reject('maxWidth');
return;
}
if (maxHeight + deviation < imgHeight) {
reject('maxHeight');
return;
}
resolve();
};
image.src = src;
image.onerror = reject;
};
filereader.readAsDataURL(file);
});
}
var _default = checkImageFileWH;
exports.default = _default;