forked from csbun/resize-image
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (106 loc) · 3.31 KB
/
index.js
File metadata and controls
113 lines (106 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(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 <img> or Image() or <canvas>
* @param {number} width output image width
* @param {number} height output image height
* @return {array<number>} [ 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 <img> or <canvas> to canvas
* @param {Image} img an <img> or Image() or <canvas>
* @param {number} width output image width
* @param {number} height output image height
* @return {Canvas} output image canvas
*/
function resize2Canvas(img, width, height) {
if (!img) {
throw new Error('`img` is required.');
}
// 计算新图片的宽高
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 <img> or <canvas> to base64
* @param {Image} img an <img> or Image() or <canvas
* @param {number} width output image width
* @param {number} height output image height
* @param {string} type output image type
* @return {string} output image base64 string
*/
out.resize = function resize(img, width, height, type) {
if (IMG_TYPE.indexOf(type) < 0) {
type = IMG_TYPE_PNG;
}
var canvas = resize2Canvas(img, width, height);
var ctx = canvas.getContext('2d');
// set backgrund color to #fff while output type is NOT PNG
if (type !== IMG_TYPE_PNG) {
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalCompositeOperation = '';
}
return canvas.toDataURL('image/' + type);
};
return out;
});