PHP实现网页截图的方法

发布时间:2020-08-17 11:22:34 作者:小新
来源:亿速云 阅读:864

小编给大家分享一下PHP实现网页截图的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

PHP实现网页截图的方法:首先下载CutyCapt;然后解压tar包;最后使用代码“public function index(){echo '<pre>';echo date('Y-m-d H:m:s')...}”实现截图即可。

PHP实现网页截图的方法

如果需要将一个网址的内容做成视图图片输出,则需要辅助工具CutyCapt来实现这一需求

Windows系统

 public function index()
    {
        echo '<pre>';
        echo date('Y-m-d H:m:s').'<br>';
        $_GET['url'] = 'https://www.aliyun.com/jiaocheng/970387.html';
        $url=$_GET["url"];
        $imgName = rand(10000,99999);
        $out = 'D:/game/'.$imgName.'.png'; //输出图片地址
        $path = 'D:/game/CutyCapt.exe';   //工具存储地址
        $cmd = "$path --url=$url --out=$out";
        echo $cmd.'<br>';
        system($cmd, $em);
        if(!empty($em)){
            echo '成功了呢';
        };
        echo date('Y-m-d H:m:s').'<br>';

    }

QIODevice::read: maxSize argument exceeds QByteArray size limit terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

处理
1、开始–运行–输入cmd
2、输入 cd %windir%\system32 (进入windows安装目录的system32文件夹)
3、输入 Regsvr32 Msxml3.dll
4、缘由链接

Linux系统

CutyCapt是一个截图工具

官方网站 http://cutycapt.sourceforge.net/
我主要的目的是解决用php保存网页为图片,废话不多少开始安装CutyCapt

yum install epel-release
#依赖安装
yum install Xvfbyum install xorg-x11-fonts*yum install qtwebkit-devel qt-devel
#中文字体
yum install fonts-chinese
yum install CutyCapt
#测试使用
xvfb-run --server-args="-screen 0, 1920x1080x24" CutyCapt --url=http://www.phpsong.com --out=phpsong.jpg
如果提示错误libEGL warning: DRI2: failed to open swrast (search paths /usr/lib64/dri)

解决方法

yum install mesa-dri-drivers

问题

1)截图时候有乱码
2) 执行速度慢
3)截取百度和小松博客,发现按钮上的文字没有显示,应该也是乱码导致的

PS.中文乱码问题

将windows下的中文字体上传至/usr/share/fonts目录,执行下命令fc-cache即可

测试截图

xvfb-run --server-args="-screen 0, 1920x1080x24" CutyCapt --url=http://www.phpsong.com --out=1.jpg
xvfb-run --server-args="-screen 0, 1920x1080x24" CutyCapt --url=http://www.baidu.com --out=2.jpg
xvfb-run --server-args="-screen 0, 1920x1080x24" CutyCapt --url=http://www.163.com --out=3.jpg
xvfb-run --server-args="-screen 0, 1920x1080x24" CutyCapt --url=http://www.sina.com.cn --out=4.jpg

例子

<?php
/* 
网页截图功能,必须安装IE+CutyCapt
url:要截图的网页
out:图片保存路径
path:CutyCapt路径
cmd:CutyCapt执行命令
比如:http://你php路径.php?url=http://www.111cn.net
*/
$url=$_GET["url"];
$imgname=str_replace('http://','',$url);
$imgname=str_replace('https://','',$imgname);
$imgname=str_replace('.','-',$imgname);
$out = 'D:/webroot/test/'.$imgname.'.png';
$path = 'D:/webserver/CutyCapt.exe';
$cmd = "$path --url=$url --out=$out";
echo $cmd;
system($cmd);
?>

以上是PHP实现网页截图的方法的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. html基于canvas实现截图的方法
  2. 使用JavaScript怎么实现网页截图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php 截图

上一篇:解决php json_encode中文乱码的问题

下一篇:yum安装php memcached的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》