-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
190 lines (187 loc) · 5.94 KB
/
index.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict'
const KOCReturn = require('koc-common-return/index')
const ImageMagick = require('gm').subClass({imageMagick: true})
const KOCImage = {
Ratio: (Path) => {
return new Promise((resolve) => {
ImageMagick(Path).size((err, value) => {
const retValue = KOCReturn.Value()
if (err) {
retValue.hasError = true
retValue.message = err.message
retValue.returnObject = err
return resolve(retValue)
}
retValue.returnObject = value.width / value.height
retValue.PutValue('Width', value.width)
retValue.PutValue('Height', value.height)
resolve(retValue)
})
})
},
Resize: (mode, source, target, width, height) => {
return new Promise((resolve) => {
const retValue = KOCReturn.Value()
let Image = ImageMagick(source)
if (!Image) {
retValue.hasError = true
retValue.message = '没有找到图片文件'
return resolve(retValue)
}
height = (height && height > 0) ? height : null
Image.size((err, value) => {
if (err) {
retValue.hasError = true
retValue.message = err.message
return resolve(retValue)
}
let x = 0, y = 0, ow = value.width, oh = value.height, tw = width, th = height
const Ratio = {
Width: value.width,
Height: value.height,
Original: value.width / value.height,
This: -1
}
switch (mode) {
case 'W'://固定宽
case 'H'://固定高
case 'W_H'://最大宽,最大高(比例)
Image.resize(tw, th)
Ratio.This = tw / th
break
default://固定宽,固定高
Image.resize(tw, th, '!')
Ratio.This = tw / th
break
case 'Cut_Middle'://裁剪(中)(按比例放大or缩小)
if (!width || !height) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
th = oh * tw / ow
if (height > th) {
tw = tw * height / th
th = height
}
Image = Image.resize(tw, th).crop(width, height, (tw - width) / 2, (th - height) / 2)
Ratio.This = width / height
break
case 'Cut_Top'://裁剪(顶)(按比例放大or缩小)
if (!width || !height) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
th = oh * tw / ow
if (height > th) {
tw = tw * height / th
th = height
}
Image = Image.resize(tw, th).crop(width, height, (tw - width) / 2, 0)
Ratio.This = width / height
break
case 'Cut_Bottom'://裁剪(顶)(按比例放大or缩小)
if (!width || !height) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
th = oh * tw / ow
if (height > th) {
tw = tw * height / th
th = height
}
Image = Image.resize(tw, th).crop(width, height, (tw - width) / 2, th - height)
Ratio.This = width / height
break
case 'W_HMaxCut'://固定宽,最大高裁剪(顶)
if (!height) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
th = oh * width / ow
Image = Image.resize(tw, th)
Ratio.This = tw / th
if (th > height) {
Image = Image.crop(tw, height, x, y)
Ratio.This = tw / height
}
break
case 'W_HMaxCut_Middle'://固定宽,最大高裁剪(中)
if (!height) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
th = oh * width / ow
Image = Image.resize(tw, th)
Ratio.This = tw / th
if (th > height) {
y = (th - height) / 2
Image = Image.crop(tw, height, x, y)
Ratio.This = tw / height
}
break
case 'H_WMaxCut'://固定高,最大宽裁剪(顶)
if (!width) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
tw = ow * height / oh
Image = Image.resize(tw, th)
Ratio.This = tw / th
if (tw > width) {
Image = Image.crop(width, th, x, y)
Ratio.This = width / th
}
break
case 'H_WMaxCut_Middle'://固定高,最大宽裁剪(顶)
if (!width) {
Image.resize(tw, th)
Ratio.This = tw / th
break
}
tw = ow * height / oh
Image = Image.resize(tw, th)
Ratio.This = tw / th
if (tw > width) {
x = (tw - width) / 2
Image = Image.crop(width, th, x, y)
Ratio.This = width / th
}
break
}
if (typeof target !== 'string') {
target = 'JPG'
}
if (['JPG', 'PNG'].includes(target)) {
Image.toBuffer(target, (err, buffer) => {
if (err) {
retValue.hasError = true
retValue.message = err.message
return resolve(retValue)
}
retValue.returnObject = buffer
retValue.PutValue('Ratio', Ratio)
resolve(retValue)
})
} else {
Image.write(target, (err) => {
if (err) {
retValue.hasError = true
retValue.message = err.message
return resolve(retValue)
}
retValue.returnObject = Ratio
retValue.PutValue('Ratio', Ratio)
resolve(retValue)
})
}
})
})
}
}
module.exports = KOCImage