php网页截图保存

使用CutyCapt实现网页截图保存

windows 环境下使用

下载地址:https://sourceforge.net/projects/cutycapt/files/cutycapt/
我下载的是 CutyCapt-Win32-2010-06-06.zip,下载后解压保存

<?php
set_time_limit(0);
$s=time();
$url='http://www.baidu.com';	//要截图的网页
$imgname='test.png';		
$out = 'D:/'.$imgname;		//图片保存路径
$path = 'D:/CutyCapt.exe';	//CutyCapt路径
$cmd = "$path --url=$url --out=$out";	//CutyCapt执行命令
echo $cmd , '<br>';	//D:/CutyCapt.exe --url=http://www.baidu.com --out=D:/test.png
system($cmd);
$e=time();
echo '耗时:' . ($e - $s) . '秒';

linux环境下使用(ubuntu16.04系统)

#sudo apt install cutycapt/xenial
#cutycapt --url=http://www.baidu.com --out=baidu.jpg  
普通用户执行或者su - root执行 (su切换到root执行会报错)
(cutycapt:20501): GLib-GIO-CRITICAL **: g_dbus_connection_get_unique_name:
 assertion 'G_IS_DBUS_CONNECTION (connection)' failed

使用PhantomJS实现网页截图保存

PhantomJS 是一个基于 WebKit 的服务器端 JavaScript API。它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。PhantomJS 可以用于 页面自动化,网络监测,网页截屏,以及无界面测试等。
这里只用网页截屏功能,更详细的功能参考

下载

安装包下载地址: http://phantomjs.org/download.html
包括 Windows ,Mac OS,Linux版本

windows 安装

下载 phantomjs-2.1.1-windows.zip,解压后保存。主要有两个目录bin(exe程序) 和examples(各种demo)

linux(ubuntu16.04) 安装

运行以下命令
#cd /home/test
#wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
#tar -xf phantomjs-2.1.1-linux-x86_64.tar.bz2		
#mv phantomjs-2.1.1-linux-x86_64 phantomjs   
#cd phantomjs/examples/
#vim test.js		//在examples下创建test.js,将下面test.js的代码copy进去
以命令行方式运行,下面的指令是在test.js目录下即/home/test/phantomjs/examples/目录下运行的
运行成功后能在 /home/test目录下 看到test.png就安装成功了
#/home/test/phantomjs/bin/phantomjs  test.js   http://www.baidu.com   /home/test/test.png

创建test.php

<?php
set_time_limit(0);
$path = 'D:\phantomjs-2.1.1-windows\bin\phantomjs';		//phantomjs路径
//$path = '/home/test/phantomjs/bin/phantomjs';	//linux目录
$jsPath = 'D:\phantomjs-2.1.1-windows\examples\test.js';	//test.js路径
//$jsPath = '/home/test/phantomjs/examples/test.js';	//linux目录
$url = 'https://blog.csdn.net/';		//要抓取的网页
$out = 'D:/test222.png';			//图片保存路径
//$out = '/home/test222.png';	//确认php具有该目录写入权限	
$cmd = "$path $jsPath $url $out";
echo $cmd;
system($cmd);

在examples下创建test.js

var page = require('webpage').create();
var args = require('system').args;
var url = args[1];
var filename = args[2];
page.open(url, function () {
    page.render(filename);
    phantom.exit();
});

两种方法对比

PhantomJS比CutyCapt更加稳定,CutyCapt无法截取https的网页,而PhantomJS可以
CutyCapt使用更简单,PhantomJS需要会nodejs
两种方法都不能截取VUE之类前端框架写的页面

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值