forked from opsb/node-webshot-server
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi.js
More file actions
70 lines (63 loc) · 2.68 KB
/
api.js
File metadata and controls
70 lines (63 loc) · 2.68 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
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
var fs = require('fs');
var temp = require('temp');
var webshot = require('webshot');
/**
* Generates a PNG image for a URL
*
* @param {String} url The URL for which to generate a PNG image
* @param {Object} [options] A set of options that manipulate the image
* @param {Number} [options.width] The desired width (in pixels) for the generated image, default: 1024
* @param {Number} [options.height] The desired height (in pixels) for the generated image, default 768
* @param {Number} [options.delay] The delay (in milliseconds) before the screenshot, default 0, maximum 10000
* @param {String} [options.userAgent] An optional user agent, defaults to an empty string
* @param {Boolean} [options.full] If specified, the entire webpage will be screenshotted and the `options.height` property will be ignored
* @param {Function} callback A standard callback function
* @param {Object} callback.err An error object (if any)
* @param {String} callback.path The path on disk where the image is stored
*/
var generate = module.exports.generate = function(url, options, callback) {
options = options || {};
options.width = options.width || 1024;
options.height = options.height || 768;
options.delay = options.delay || 0;
options.userAgent = options.userAgent || '';
if (options.delay > 10000) {
options.delay = 10000;
}
screengrab(url, options, callback);
};
/**
* Take a screenshot of url
*
* @param {String} url The URL for which to generate a PNG image
* @param {Object} options A set of options that manipulate the page
* @param {Function} callback A standard callback function
* @param {Object} callback.err An error object (if any)
* @param {String} callback.path The path on disk where the image is stored
* @api private
*/
var screengrab = function(url, options, callback) {
var tempPath = temp.path({suffix: '.png'});
var webshotOptions = {
'renderDelay': options.delay,
'windowSize': {
'width': options.width,
'height': options.height
},
'shotSize': {
'width': 'window',
'height': (options.full === true) ? 'all' : 'window'
},
'userAgent': options.userAgent,
'phantomConfig': {
'ignore-ssl-errors': true,
'ssl-protocol': 'any'
}
};
webshot(url, tempPath, webshotOptions, function(err) {
if (err) {
return callback({'code': 500, 'msg': 'Unable to take a screenshot'});
}
return callback(null, tempPath);
});
};