(function(root, factory) { 'use strict'; /* istanbul ignore else */ if (typeof exports === 'object') { // CommonJS module.exports = factory(); } else if (typeof define === 'function' && define.amd) { // AMD define(function() { return factory(); }); } else if (typeof define === 'function' && define.cmd) { // CMD define(function(require, exports, module) { module.exports = factory(); }); } else { // Global Variables root.ResizeImage = factory(); } })(this, function () { 'use strict'; var out = {}; var IMG_TYPE_PNG = 'png'; var IMG_TYPE = [IMG_TYPE_PNG, 'gif', 'bmp', 'jpeg', 'webp']; for (var i = 0; i < IMG_TYPE.length; i++) { out[IMG_TYPE[i].toUpperCase()] = IMG_TYPE[i]; } /** * 计算新图片宽高 * @private * @param {Image} img an or Image() or * @param {number} width output image width * @param {number} height output image height * @return {array} [ width, height ] */ function getNewImageDimentions(img, width, height) { var detImg = img.width / img.height; if (width > 0 && height > 0) { // 同时指定了宽高,按原图缩放 if (width / height > detImg) { height = width / detImg; } else { width = height * detImg; } return [ width, height ]; } else if (width > 0) { // 只指定宽度的情况 return [ width, width / detImg ]; } else if (height > 0) { // 只指定高度的情况 return [ height * detImg, height ]; } else { // 否则原 size 返回 return [ img.width, img.height ]; } } /** * resize an or to canvas * @param {Image} img an or Image() or * @param {number} width output image width * @param {number} height output image height * @return {Canvas} output image canvas */ function resize2Canvas(img, width, height) { if (!img) { return img; } // 计算新图片的宽高 var newImageDimentions = getNewImageDimentions(img, width, height); width = newImageDimentions[0]; height = newImageDimentions[1]; // 画到 canvas 中 var canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; var ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0, width, height); return canvas; } out.resize2Canvas = resize2Canvas; /** * resize an or to base64 * @param {Image} img an or Image() or