PHP 截图网页截图html转为图片通过PhantomJS截图支持thinkphp和ci框架或yii

如果页面有JS错误,则会终止执行。所以我在每个html页面head里加上这句屏蔽提示:

$content = str_replace("<head>",'<head><script type="text/javascript">window.onerror=function(){return true;}</script>',$content);

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下载

#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
运行以下命令
#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();
});

https://blog.csdn.net/bizu005/article/details/84025569

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值