• Stars
    star
    124
  • Rank 282,529 (Top 6 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

网页截图工具 (by phantomjs)

web-camera

网页截图工具 (by phantomjs)。通过phantomjs来打开渲染网页,对网页进行截图。

Usage

var Camera = require('webcamera');
var fs = require('fs');

/**
 * web camera by Node.js and Phantomjs
 * @param {Object} options
 *   - path          {String}   default picture dir path
 *   - workerNum     {Number}   child_process max num
 *   - timeout       {Number}   child_process timeout.
 *   - phantom       {String}   phantomjs path
 *   - phantomScript {String}   phantomjs script path, use input arguments as default script
 *   - qnClient      {Object}   qiniu client instance
 *   - qnOpts        {Object}   qiniu options. if do not have qnClient and qnOpts, shotQN become invalid
 */
var camera = Camera.create({
  qnOpts: {
    accessKey: 'accessKey',
    secretKey: 'secretKey',
    bucket: 'webcamera'
  }
});

// 当处理速度比调用速度低时会触发此事件
camera.on('overload', function (listLength) {
  //listLength为排队等待处理的长度
});

// 截图保存到本地
camera.shot('http://www.baidu.com', './baidu.png', function (err, data) {
  // data.should.equal('./baidu.png');
});

// 截图作为stream
camera.shotStream('http://www.baidu.com', function (err, s) {
  var datas = [];
  var filePath = './test.jpg';
  var file = fs.createWriteStream(filePath, {encoding: 'binary'});
  s.on('data', function (data) {
    file.write(data.toString('binary'), 'binary');
  });
  s.on('end', function () {
    console.log('get pictrue ok');
  });
});

//截图上传到七牛空间,第二个参数为上传七牛的options,第三个参数是截图的options
camera.shotQN('http://www.baidu.com', {key: 'test/baidu.png'}, {quality: 10}, function (err, data) {
  /*
  data.should.like:
  ({
    hash: 'FlDGti9pVGQ3sw2oao-mVu3nZWjZ',
    key: 'test/baidu.png',
    url: 'http://webcamera.u.qiniudn.com/test/baidu.png'
  })
  */
});

所有的调用都可以在callback之前传入参数options.

camera.shotQN('http://www.baidu.com',320, 'baidu.png', {
  clipRect: {
    top: 0,
    left:0,
    height: 'all',
    width: 'all'
  }
}, function (err, data) {
  /*
  data.should.like:
  {name: 'L1/1/320/baidu.png', size: 36889, url: 'xxx/L1/1/320/baidu.png'}
  */
});
名字 类型 含义
clipRect Object 指定截图的矩形区域。有四个属性:top(0), left(0), height(window), width(window)。height和width可以设置为window或者all,window将会截取当前一屏,all会截取网页全部大小
viewportSize Object 设置网页的分辨率,有两个属性:width(1024), height(768)。
renderDelay Number 网页加载完成之后延迟多少毫秒之后截图,默认为0
picPath String 设置图片保存位置,只在shot方法时生效,等效于shot方法的第二个参数
mimeType String 设置截图的保存类型(只有在没设置图片保存路径的情况下生效,否则使用图片保存路径的后缀类型),支持png, jpeg, gif.默认为png
script Function 网页加载完成之后可以在网页中执行这个方法。
quality Number 0~100,指定生成图片的质量,数值越高质量越好

Install

$ npm install webcamera

Dependences

Debug

$ tail -f /tmp/phantom_shot.log &
$ phantomjs "phantom/web_camera_phantom.js" "https://github.com/" > github.png

github page screen shot: github.png

command line tool

npm install webcamera -g, 之后可以使用web camera 提供的命令行工具进行网页截图。需要安装phantomjs

camera -u http://www.google.com -o google.png

Options:
  -u, --url     Web's url
  -o, --out     Output screenshot picture path                                                  [default: "./out.png"]
  -c, --config  config file path, you can define much more options by a js file or a json file

可以通过传递配置文件的方式进行批量截图,并对截图的详细参数进行设置,配置文件可以是json文件或者js文件,配置文件模版:

#json形式
[{
  "url": "http://cnodejs.org",
  "out": "cnodejs.png"
}, {
  "url": "http://nodejs.org",
  "out": "nodejs.png",
  "options": {
    "viewportSize": {
      "width": 1280,
      "height": 800
    }
  }
}, {
  "url": "http://google.com",
  "out": "google.png",
  "options": {
    "quality": 10
  }
}]

#js形式
var url = 'http://google.com';

var config = [];

for (var quality = 10; quality <= 100; quality += 10) {
  config.push({
    url: url,
    out: 'google@' + quality + '.jpg',
    options: {
      quality: quality
    }
  });
}

module.exports = config;

推荐通过js形式的配置文件,将会更加灵活。

Licences

(The MIT License)

Copyright (c) 2013 dead-horse and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

utility

A collection of useful utilities.
TypeScript
1,239
star
2

parameter

A parameter verify tools.
JavaScript
727
star
3

urllib

Request HTTP(s) URLs in a complex world.
TypeScript
725
star
4

agentkeepalive

Support keepalive http agent.
JavaScript
571
star
5

emoji

emoji: This library allows the handling and conversion of Emoji in Javascript.
HTML
502
star
6

weibo

weibo nodejs sdk
JavaScript
489
star
7

compressing

Everything you need for compressing and uncompressing
JavaScript
424
star
8

detect-port

Node.js module to detect available ports of the system.
JavaScript
376
star
9

qn

Another qiniu http://docs.qiniu.com/api/ API client for Node.js.
JavaScript
274
star
10

graceful

Graceful exit when `uncaughtException` emit, base on `process.on('uncaughtException')`.
JavaScript
249
star
11

address

Get current machine IP and MAC address.
TypeScript
228
star
12

autod

Auto generate dependencies and devDependencies by parse the project file.
JavaScript
205
star
13

js-to-java

easy way to wrap js object to java object
JavaScript
201
star
14

common-bin

Abstraction bin tool
JavaScript
189
star
15

hessian.js

js hessian binary web service protocol, support communicate with java
JavaScript
183
star
16

mm

An simple but flexible mock(or say stub) package, mock mate
JavaScript
158
star
17

cfork

cluster fork and restart easy way
JavaScript
156
star
18

formstream

multipart/form-data encoded stream, helper for file upload.
JavaScript
145
star
19

urlencode

encodeURIComponent with charset
TypeScript
142
star
20

cluster-client

Sharing Connection among Multi-Process Nodejs
JavaScript
131
star
21

is-type-of

complete type checking for node
TypeScript
122
star
22

coffee

Test command line on Node.js
JavaScript
97
star
23

clet

Command Line E2E Testing
JavaScript
76
star
24

byte

Input Buffer and Output Buffer, just like Java ByteBuffer
JavaScript
76
star
25

sdk-base

a base class for sdk with default error handler
JavaScript
62
star
26

china-mobile-phone-regexp

The regular expressions for China mobile phone number.
JavaScript
60
star
27

mz-modules

Same as `mz`, but wrap modules in the world rather than core API.
JavaScript
56
star
28

thunkify-wrap

a more powerful thunkify
JavaScript
55
star
29

ots

This project no longer maintained, please use https://github.com/ali-sdk/ots2 instead.
JavaScript
48
star
30

urllib-sync

sync http request
JavaScript
44
star
31

stream-wormhole

Pipe ReadStream to a wormhole
TypeScript
42
star
32

copy-to

copy an object's properties to another one
JavaScript
41
star
33

optimized

Detect a function can be optimized or not.
JavaScript
41
star
34

runscript

Run script easy!
JavaScript
41
star
35

await-stream-ready

a promise waiting stream ready
JavaScript
34
star
36

charset

Get the content charset from header and html content-type.
JavaScript
34
star
37

ab

A benchmark tool.
JavaScript
33
star
38

npm-updater

Prompt update of npm package
JavaScript
32
star
39

java.io

A Node.js implement of "java.io.InputObjectStream.readObject()" and "java.io.OutputObjectStream.writeObject()"
JavaScript
31
star
40

humanize-ms

transform humanize time to ms
JavaScript
30
star
41

graceful-process

graceful exit process even parent exit on SIGKILL.
JavaScript
29
star
42

node-murmurhash

murmurhash, support utf8 Buffer
JavaScript
27
star
43

sendmessage

Send a cross process message if message channel is connected.
JavaScript
27
star
44

shameimaru

Shameimaru Aya likes to traverse node_modules and capture the tree.
JavaScript
26
star
45

ready-callback

Launch server after all async task ready
TypeScript
26
star
46

tcp-base

TCP client base
JavaScript
25
star
47

serialize-json

A serialize algorithm for JSON
JavaScript
23
star
48

js-remoting-for-apache-dubbo

dubbo protocol implement
JavaScript
22
star
49

loading

loading dir files as module to an object
JavaScript
20
star
50

zookeeper-cluster-client

Sharing one zookeeper connection among Multi-Process on Node.js
JavaScript
20
star
51

tnpm

JavaScript
19
star
52

enable

Detect es6 features enable or not.
JavaScript
19
star
53

last-commit-log

Node.js module to get the last git commit information - mostly to be used by CI/CD and building phase.
JavaScript
19
star
54

jdwp

Java Debug Wire Protocol
JavaScript
18
star
55

time-profile

Measuring execution time of functions
JavaScript
16
star
56

qps

High performance qps counter
JavaScript
15
star
57

cluster-reload

Easy and safe reload your workers.
JavaScript
14
star
58

await-first

Wait the first event in a set of event pairs, then clean up after itself.
JavaScript
14
star
59

buffer-type

Detect content-type from Buffer data.
JavaScript
14
star
60

pedding

Just pedding for callback.
JavaScript
13
star
61

should2assert

A tool to help you migrating from should to assert
JavaScript
13
star
62

save-file-atomic

Save anything to file atomic
JavaScript
13
star
63

wt

wt: Simple dir watcher, support events
JavaScript
13
star
64

zlogger

The last console logger
JavaScript
12
star
65

logfilestream

Log stream, including auto rolling feature, support multiprocess `append` write at the same time.
JavaScript
12
star
66

dns-hijacking

JavaScript
11
star
67

default-user-agent

Default user agent string for Node.js http request
JavaScript
11
star
68

jsonp-body

Helper to create more safe jsonp response body for koa and other web framework.
JavaScript
10
star
69

zookeeper-watcher

Extend node-zookeeper-client, let zookeeper client support `watch(path)` method.
JavaScript
10
star
70

weibo-mid

Convert Weibo Open API mid to base62 hash string.
JavaScript
10
star
71

connection

JavaScript
10
star
72

enums

JavaScript
9
star
73

node-homedir

Get the effective user's homedir
TypeScript
9
star
74

rid

Request Unique ID, base on current server `ip`, `process.pid` and auto increase id, like `$ip,$pid,$id`
JavaScript
9
star
75

error-formatter

format an error into string or json for log
JavaScript
8
star
76

auto-correct

auto correct for command line tools
JavaScript
8
star
77

install-node

Install node into node_modules
JavaScript
8
star
78

digest-header

Digest access authentication header helper
JavaScript
8
star
79

http-doctor

HTTP doctor
JavaScript
7
star
80

vmto

velocity(vm) to any other templates.
JavaScript
7
star
81

restful-client

RESTFul api client base object. Usually use by some api client implementation.
JavaScript
6
star
82

diskstore

a local cache implement
JavaScript
6
star
83

koa-override

method override middleware for koa
JavaScript
6
star
84

memory-tripwire

suicide when memory over the limit
JavaScript
5
star
85

urlmock

Easy load mock data from a specify url.
JavaScript
5
star
86

performance-ms

performance timer in ms
TypeScript
4
star
87

common-bin-plus

advanced common-bin for cli usage
JavaScript
4
star
88

aggregate-base

Base class for aggregate operation
JavaScript
4
star
89

watch-files

watch files by polling
JavaScript
3
star
90

oss-syncer

synchronization all files from this bucket to another bucket
JavaScript
3
star
91

common-boilerplate

base class for boilerplate
JavaScript
3
star
92

fileloader

more stable file loader for nunjucks, and support charsets like `gbk`.
JavaScript
3
star
93

assert-file

assert file utilities
JavaScript
3
star
94

changing

Watching `file`, `dir` or `link` changing, using interval timer.
JavaScript
3
star
95

speeds

Speeds controllable stream. You can control the downstream speed on your own.
JavaScript
3
star
96

cov

Mocha Coverage report store on CDN.
JavaScript
3
star
97

onelogger

The only ONE logger
TypeScript
3
star
98

sse-decoder

SSE decoder for nodejs
TypeScript
3
star
99

fast-ua-parser

A Fast JavaScript library to detect user's Browser, Engine, OS, CPU, and Device type/model
2
star
100

get-ready

NodeJS mixin to add one-time ready event
TypeScript
2
star