-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathimage-processing.js
210 lines (192 loc) · 7.36 KB
/
image-processing.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { _app } from '/imports/lib/core.js';
import { check } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import fs from 'fs-extra';
import gm from 'gm';
//Some platforms may bundle ImageMagick into their tools (like Heroku). In this case you may use GraphicsMagick as Imagemagick in this way:
//npm install gm --save and then where you use it:
//const gm = require('gm');
//const im = gm.subClass({ imageMagick: true });
//Please note that GM was considered slightly faster than IM so before you chose convenience over performance read the latest news about it.
//https://mazira.com/blog/comparing-speed-imagemagick-graphicsmagick
const bound = Meteor.bindEnvironment((callback) => {
return callback();
});
_app.createThumbnails = (collection, fileRef, cb) => {
check(fileRef, Object);
let isLast = false;
const finish = (error) => {
bound(() => {
if (error) {
console.error('[_app.createThumbnails] [finish]', error);
cb && cb (error);
} else {
if (isLast) {
cb && cb(void 0, fileRef);
}
}
return true;
});
};
fs.exists(fileRef.path, (exists) => {
bound(() => {
if (!exists) {
throw new Meteor.Error('File ' + fileRef.path + ' not found in [createThumbnails] Method');
}
const image = gm(fileRef.path);
const sizes = {
preview: {
width: 400
},
thumbnail40: {
width: 40,
square: true
}
};
image.size((error, features) => {
bound(() => {
if (error) {
console.error('[_app.createThumbnails] [forEach sizes]', error);
finish(new Meteor.Error('[_app.createThumbnails] [forEach sizes]', error));
return;
}
let i = 0;
collection.collection.update(fileRef._id, {
$set: {
'meta.width': features.width,
'meta.height': features.height,
'versions.original.meta.width': features.width,
'versions.original.meta.height': features.height
}
}, _app.noop);
Object.keys(sizes).forEach((name) => {
const size = sizes[name];
const path = (collection.storagePath(fileRef)) + '/' + name + '-' + fileRef._id + '.' + fileRef.extension;
const copyPaste = () => {
fs.copy(fileRef.path, path, (fsCopyError) => {
bound(() => {
if (fsCopyError) {
console.error('[_app.createThumbnails] [forEach sizes] [fs.copy]', fsCopyError);
finish(fsCopyError);
return;
}
const upd = { $set: {} };
upd.$set[`versions.${name}`] = {
path: path,
size: fileRef.size,
type: fileRef.type,
extension: fileRef.extension,
meta: {
width: features.width,
height: features.height
}
};
collection.collection.update(fileRef._id, upd, (colUpdError) => {
++i;
if (i === Object.keys(sizes).length) {
isLast = true;
}
finish(colUpdError);
});
});
});
};
if (/png|jpe?g/i.test(fileRef.extension)) {
const img = gm(fileRef.path)
.quality(70)
.define('filter:support=2')
.define('jpeg:fancy-upsampling=false')
.define('jpeg:fancy-upsampling=off')
.define('png:compression-filter=5')
.define('png:compression-level=9')
.define('png:compression-strategy=1')
.define('png:exclude-chunk=all')
.autoOrient()
.noProfile()
.strip()
.dither(false)
.interlace('Line')
.filter('Triangle');
const updateAndSave = (upNSaveError) => {
bound(() => {
if (upNSaveError) {
console.error('[_app.createThumbnails] [forEach sizes] [img.resize]', upNSaveError);
finish(upNSaveError);
return;
}
fs.stat(path, (fsStatError, stat) => {
bound(() => {
if (fsStatError) {
console.error('[_app.createThumbnails] [forEach sizes] [img.resize] [fs.stat]', fsStatError);
finish(fsStatError);
return;
}
gm(path).size((gmSizeError, imgInfo) => {
bound(() => {
if (gmSizeError) {
console.error('[_app.createThumbnails] [forEach sizes] [img.resize] [fs.stat] [gm(path).size]', gmSizeError);
finish(gmSizeError);
return;
}
const upd = { $set: {} };
upd.$set[`versions.${name}`] = {
path: path,
size: stat.size,
type: fileRef.type,
extension: fileRef.extension,
name: fileRef.name,
meta: {
width: imgInfo.width,
height: imgInfo.height
}
};
collection.collection.update(fileRef._id, upd, (colUpdError) => {
++i;
if (i === Object.keys(sizes).length) {
isLast = true;
}
finish(colUpdError);
});
});
});
});
});
});
};
if (!size.square) {
if (features.width > size.width) {
img.resize(size.width).interlace('Line').write(path, updateAndSave);
} else {
copyPaste();
}
} else {
let x = 0;
let y = 0;
const widthRatio = features.width / size.width;
const heightRatio = features.height / size.width;
let widthNew = size.width;
let heightNew = size.width;
if (heightRatio < widthRatio) {
widthNew = (size.width * features.width) / features.height;
x = (widthNew - size.width) / 2;
}
if (heightRatio > widthRatio) {
heightNew = (size.width * features.height) / features.width;
y = (heightNew - size.width) / 2;
}
img
.resize(widthNew, heightNew)
.crop(size.width, size.width, x, y)
.interlace('Line')
.write(path, updateAndSave);
}
} else {
copyPaste();
}
});
});
});
});
});
return true;
};