forked from scionoftech/webp-converter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpconverter_test.js
194 lines (145 loc) · 6.86 KB
/
webpconverter_test.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
var exec = require('child_process').execFile;//get child_process module
var enwebp = require('./cwebp_test.js');//get cwebp module(converts other image format to webp)
var dewebp = require('./dwebp_test.js');//get dwebp module(converts webp format to other image)
var gifwebp = require('./gwebp_test.js');//get gif2webp module(convert git image to webp)
var webpmux = require('./webpmux_test.js');//get webpmux module(convert non animated webp images to animated webp)
/******************************************************* cwebp *****************************************************/
//now convert image to .webp format
module.exports.cwebp = function (input_image, output_image, option, callback) {
// input_image: input image(.jpeg, .pnp ....)
//output_image: output image .webp
//option: options and quality,it should be given between 0 to 100
var query = option + ' ' + input_image + ' -o ' + output_image;//command to convert image
console.log(query);
//enwebp() return which platform webp library should be used for conversion
exec(enwebp(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
/******************************************************* dwebp *****************************************************/
//now convert .webp to other image format
module.exports.dwebp = function (input_image, output_image, option, callback) {
// input_image: input image .webp
//output_image: output image(.jpeg, .pnp ....)
//option: options and quality,it should be given between 0 to 100
var query = input_image + ' ' + option + ' ' + output_image;//command to convert image
console.log(query);
//dewebp() return which platform webp library should be used for conversion
exec(dewebp(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
/******************************************************* gif2webp *****************************************************/
//now convert .gif image to .webp format
module.exports.gwebp = function (input_image, output_image, option, callback) {
// input_image: input image(.jpeg, .pnp ....)
//output_image: /output image .webp
//option: options and quality,it should be given between 0 to 100
var query = option + ' ' + input_image + ' -o ' + output_image;//command to convert image
//gifwebp() return which platform webp library should be used for conversion
exec(gifwebp(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
/******************************************************* webpmux *****************************************************/
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Add ICC profile,XMP metadata and EXIF metadata
module.exports.webpmux_add = function (input_image, output_image, icc_profile, option, callback) {
// input_image: input image(.webp)
//output_image: output image .webp
//icc_profile: icc profile
//option: get or set option (icc,xmp,exif)
var query = '-set ' + option + ' ' + icc_profile + ' ' + input_image + ' -o ' + output_image;
//webpmux() return which platform webp library should be used for conversion
exec(webpmux(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Extract ICC profile,XMP metadata and EXIF metadata
module.exports.webpmux_extract = function (input_image, icc_profile, option, callback) {
// input_image: input image(.webp)
//icc_profile: icc profile
var query = '-get ' + option + ' ' + input_image + ' -o ' + icc_profile;
//webpmux() return which platform webp library should be used for conversion
exec(webpmux(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Strip ICC profile,XMP metadata and EXIF metadata
module.exports.webpmux_strip = function (input_image, output_image, option, callback) {
// input_image: input image(.webp)
//output_image: output image .webp
var query = '-strip ' + option + ' ' + input_image + ' -o ' + output_image;
//webpmux() return which platform webp library should be used for conversion
exec(webpmux(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Create an animated WebP file from Webp images
module.exports.webpmux_animate = function (input_images, output_image, loop, bgcolor, callback) {
// input_images: array of image(.webp)
//output_image: animatedimage .webp
//loop:Loop the frames n number of times
//bgcolor: Background color of the canvas
var files = '-frame ' + input_images[0];
var j = input_images.length;
for (i = 1; i < j; i++) {
files = files + ' -frame ' + input_images[i];
console.log(files);
}
var query = files + ' -loop ' + loop + ' -bgcolor ' + bgcolor + ' -o ' + output_image;
//webpmux() return which platform webp library should be used for conversion
exec(webpmux(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Get the a frame from an animated WebP file
module.exports.webpmux_getframe = function (input_image, output_image, frame_number, callback) {
// input_image: input image(.webp)
//output_image: output image .webp
//frame_number: frame number
var query = '-get frame ' + frame_number + ' ' + input_image + ' -o ' + output_image;
//webpmux() return which platform webp library should be used for conversion
exec(webpmux(), query.split(/\s+/), function (error, stdout, stderr) //execute command
{
if (error) {
callback('101', error);//return error code
} else {
callback('100', 'Converted Successfully');//return success code
}
});
};