总结
豆包 AI 助手文章总结

PHP如何实现网页截图

藏色散人
发布: 2020-08-15 09:33:37
原创
5745人浏览过
PHP实现网页截图的方法:首先下载CutyCapt;然后解压tar包;最后使用代码“public function index(){echo '';echo date('Y-m-d H:m:s')...}”实现截图即可。

PHP如何实现网页截图

推荐:《PHP视频教程

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

Windows系统

  • 下载CutyCapt
    CutyCapt下载地址:http://sourceforge.net/projects/cutycapt/files/cutycapt/
  • 解压tar包,看到CutyCapt.exe  不需要再点击安装
  • 代码实现(时间需要20多秒):
 public function index()
    {
        echo '<pre class="brush:php;toolbar:false">';
        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>';

    }
登录后复制
  • 注意(如果直接命令行执行 $cmd报错):

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、缘由链接

立即学习PHP免费学习笔记(深入)”;

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如何实现网页截图的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号