PhantomJS截图工具

安装PhantomJS

直接在官网下载编译好的包就行
http://phantomjs.org/download.html

截图使用介绍

1、写一个需要被截图的页面
    用www.baidu.com做例子

2、写截图的js(test.js)
    var page = require('webpage').create(),
        system = require('system'),
        address;

    page.viewportSize = { width: 720, height: 1247 };

    // 如果命令行没有给出网址
    if (system.args.length === 1) {
        console.log('Usage: page.js <some URL>');
        phantom.exit();
    }

    address = system.args[1];
    id = system.args[2];

    page.open(address, function (status) {
        page.evaluate(function() {
            document.body.bgColor = 'white';
        });

        page.render('/tmp/'+id+'.jpeg', {format: 'jpeg', quality: '70'});

        phantom.exit();
    });

3、运行phantomjs的服务端代码(a.php)
    $str = '/usr/local/phantomjs/bin/phantomjs test.js http://www.baidu.com '.$id;
    shell_exec($str);
    注释:
        /usr/local/phantomjs/bin/phantomjs是我的命令安装路径
        test.js 是js地址
        http://www.baidu.com 是需要截图的网站
        $id     是传入的参数

文档

官网
    http://phantomjs.org/

中文
    http://javascript.ruanyifeng.com/tool/phantomjs.html
坚持原创技术分享,您的支持将鼓励我继续创作!