使用php + phantomjs截取网址的整个页面, 直接上干货。
js下载网址http://phantomjs.org/ (我下载的是1.9.7 win 版本的)
解压即可,这是目录结构
新建一个 js文件,以下是js代码
var page = new WebPage(),
address, output, size;
if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: rasterize.js URL filename');
phantom.exit();
} else {
address = phantom.args[0];
output = phantom.args[1];
page.viewportSize = { width: 600, height: 600 };
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render(output);
phantom.exit();
}, 200);
}
});
}
php代码:
$url="http://www.baidu.com";
$url = trim($url);
$filePath = "D:/libin.png";
$command = "D:/xampp/htdocs/phantomjs/phantomjs D:/xampp/htdocs/phantomjs/snap.js {$url} {$filePath}";
$a=system($command,$o);
system($command);//执行cmd命令
phantomjs 命令参数 D:/xampp/htdocs/phantomjs/phantomjs(.exe程序) D:/xampp/htdocs/phantomjs/snap.js(js文件) $url(截图的网址) $filePath(图片存放路径)
D:/xampp/htdocs/phantomjs/phantomjs --version 命令查看版本号
cmd 可以截取图片,程序不可以那就是 php程序的命令有问题。本人因为存放图片路径没写/导致纠结半天,很奇怪,用命令行就可以存图。