1 | 'use strict';
|
2 | const execBuffer = require('exec-buffer');
|
3 | const isCwebpReadable = require('is-cwebp-readable');
|
4 | const cwebp = require('cwebp-bin');
|
5 |
|
6 | module.exports = (options = {}) => input => {
|
7 | if (!Buffer.isBuffer(input)) {
|
8 | return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
|
9 | }
|
10 |
|
11 | if (!isCwebpReadable(input)) {
|
12 | return Promise.resolve(input);
|
13 | }
|
14 |
|
15 | const args = [
|
16 | '-quiet',
|
17 | '-mt'
|
18 | ];
|
19 |
|
20 | if (options.preset) {
|
21 | args.push('-preset', options.preset);
|
22 | }
|
23 |
|
24 | if (options.quality) {
|
25 | args.push('-q', options.quality);
|
26 | }
|
27 |
|
28 | if (options.alphaQuality) {
|
29 | args.push('-alpha_q', options.alphaQuality);
|
30 | }
|
31 |
|
32 | if (options.method) {
|
33 | args.push('-m', options.method);
|
34 | }
|
35 |
|
36 | if (options.size) {
|
37 | args.push('-size', options.size);
|
38 | }
|
39 |
|
40 | if (options.sns) {
|
41 | args.push('-sns', options.sns);
|
42 | }
|
43 |
|
44 | if (options.filter) {
|
45 | args.push('-f', options.filter);
|
46 | }
|
47 |
|
48 | if (options.autoFilter) {
|
49 | args.push('-af');
|
50 | }
|
51 |
|
52 | if (options.sharpness) {
|
53 | args.push('-sharpness', options.sharpness);
|
54 | }
|
55 |
|
56 | if (options.lossless) {
|
57 | args.push('-lossless');
|
58 | }
|
59 |
|
60 | if (options.nearLossless) {
|
61 | args.push('-near_lossless', options.nearLossless);
|
62 | }
|
63 |
|
64 | if (options.crop) {
|
65 | args.push('-crop', options.crop.x, options.crop.y, options.crop.width, options.crop.height);
|
66 | }
|
67 |
|
68 | if (options.resize) {
|
69 | args.push('-resize', options.resize.width, options.resize.height);
|
70 | }
|
71 |
|
72 | if (options.metadata) {
|
73 | args.push('-metadata', Array.isArray(options.metadata) ? options.metadata.join(',') : options.metadata);
|
74 | }
|
75 |
|
76 | args.push('-o', execBuffer.output, execBuffer.input);
|
77 |
|
78 | return execBuffer({
|
79 | args,
|
80 | bin: cwebp,
|
81 | input
|
82 | }).catch(error => {
|
83 | error.message = error.stderr || error.message;
|
84 | throw error;
|
85 | });
|
86 | };
|