lenye_base
Version:
基础方法
85 lines (68 loc) • 2.74 kB
JavaScript
;
require('./get_tag.js');
var is_string = require('./is_string.js');
/**
* Image optimization
* Gif images are not supported
* @param {(Object|String)} - image ,supported File Object or Data URLs
* @param {Number} [quality = 0.9] - Image quality, between 0 - 1, only image/jpeg or image/webp is accept.
* @param {Object} [options = {}] - Image options
* @param {Number} [options.maxWidth = 1920] - The maximum width of the output picture.
* If the original width of the picture is less than this width, the original size picture is returned.
* If the original width of the picture is greater than the width, the picture scaled to the size is returned.
* @param {String} [options.mimeType] - Output image type,Types of MIME.
* @returns {Object} Promise , resolve Function parameters are optimized pictures Blob Object,
* If the output type is image/gif,Then return as is image Parameter content.
*/
var imageOptimization = function (image) {
var quality = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0.9;
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$maxWidth = _ref.maxWidth,
maxWidth = _ref$maxWidth === void 0 ? 1920 : _ref$maxWidth,
mimeType = _ref.mimeType;
return new Promise((resolve, reject) => {
if (image instanceof File) {
var reader = new FileReader();
reader.onload = function () {
toBlob(this.result);
};
reader.readAsDataURL(image);
} else if (is_string(image)) {
toBlob(image);
}
/**
* To Blob
* @param {String} data - Image: Data URL
*/
function toBlob(data) {
var type = data.match(/data:([^;,]+)/);
if (Array.isArray(type)) {
var outputType = mimeType ? mimeType : type[1];
if (outputType === 'image/gif') {
return resolve(image);
}
var virtualImage = new Image();
virtualImage.src = data;
virtualImage.onload = function () {
var width = this.naturalWidth;
var height = this.naturalHeight;
if (width > maxWidth) {
height = Math.round(maxWidth * height / width);
width = maxWidth;
}
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
context.drawImage(this, 0, 0, width, height);
canvas.toBlob(blob => {
resolve(blob);
}, mimeType ? mimeType : type[1], quality);
};
} else {
reject(new Error('Non-picture type Data URLs'));
}
}
});
};
module.exports = imageOptimization;