-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (36 loc) · 1.12 KB
/
index.js
File metadata and controls
41 lines (36 loc) · 1.12 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
'use strict';
const fs = require('fs-extra');
const NestedError = require('nested-error-stacks');
const {PNG} = require('pngjs');
const OriginalPNG = require('./original-png');
const BoundedPNG = require('./bounded-png');
function parseBuffer(buffer) {
return new Promise((resolve, reject) => {
const png = new PNG();
png.parse(buffer, (err) => {
if (err) {
reject(err);
} else {
resolve(png);
}
});
});
}
exports.create = (png, {boundingBox} = {}) => {
return boundingBox
? BoundedPNG.create(png, boundingBox)
: OriginalPNG.create(png);
};
exports.fromFile = async (filePath, opts = {}) => {
try {
const buffer = await fs.readFile(filePath);
return await exports.fromBuffer(buffer, opts);
} catch (err) {
throw new NestedError(`Can't load png file ${filePath}`, err);
}
};
exports.fromBuffer = async (buffer, opts = {}) => {
const png = await parseBuffer(buffer);
return exports.create(png, opts);
};
exports.empty = (width, height) => exports.create(new PNG({width, height}));