Home > Backend Development > PHP Problem > How to implement web page screenshots in PHP

How to implement web page screenshots in PHP

藏色散人
Release: 2023-03-04 13:46:01
Original
5529 people have browsed it

How to implement web page screenshots in PHP: first download CutyCapt; then unzip the tar package; finally use the code "public function index(){echo '

';echo date('Y-m-d H:m:s ')...}" Just take a screenshot. </p></blockquote>
<p><img  src="https://img.php.cn/upload/article/202008/15/2020081509330277584.jpg" alt="How to implement web page screenshots in PHP" ></p>
<p> Recommended: "<a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">PHP Video Tutorial</a>" </p>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-963e387caa.css">
<p id="content_views" class="markdown_views prism-tomorrow-night-eighties"><!-- flowchart 箭头图标 勿删 --><svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"> </path></svg></p>
<blockquote><p>##If you need to output the content of a URL into a view image, you need an auxiliary tool <strong>CutyCapt<code> to achieve this requirement</code></strong></p></blockquote>Windows system<h2></h2>
<ul>Download CutyCapt<li>CutyCapt download address: http://sourceforge.net/projects/cutycapt/files/cutycapt/<br>
</li> Unzip the tar package and see <li>CutyCapt.exe<code> No need to click to install </code>
</li> Code implementation (it takes more than 20 seconds): <li><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;"> public function index()
    {
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        echo date(&#39;Y-m-d H:m:s&#39;).&#39;<br>&#39;;
        $_GET[&#39;url&#39;] = &#39;https://www.aliyun.com/jiaocheng/970387.html&#39;;
        $url=$_GET["url"];
        $imgName = rand(10000,99999);
        $out = &#39;D:/game/&#39;.$imgName.&#39;.png&#39;; //输出图片地址
        $path = &#39;D:/game/CutyCapt.exe&#39;;   //工具存储地址
        $cmd = "$path --url=$url --out=$out";
        echo $cmd.&#39;<br>&#39;;
        system($cmd, $em);
        if(!empty($em)){
            echo &#39;成功了呢&#39;;
        };
        echo date(&#39;Y-m-d H:m:s&#39;).&#39;<br>&#39;;

    }
Copy after login
  • Note(If $cmd is executed directly from the command line, an error will be reported):

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.

Processing:1. Start – Run – Enter cmd
2. Enter cd %windir%\system32 (enter the system32 folder in the Windows installation directory)
3. Enter Regsvr32 Msxml3.dll
4. Reason link

Linux system

CutyCapt is a screenshot tool

Official website http://cutycapt.sourceforge.net /

My main purpose is to solve the problem of using php to save web pages as pictures. Without further ado, start installing 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)
Copy after login

Solution

yum install mesa-dri-drivers
Copy after login

Problem

1) When taking screenshots There are garbled characters

2) The execution speed is slow
3) Intercepted Baidu and Komatsu blogs and found that the text on the button is not displayed, which should also be caused by garbled characters

PS. Chinese garbled code problem

Upload Chinese fonts under windows to the /usr/share/fonts directory and execute the command fc-cache

Test screenshot

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
Copy after login

Example

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

The above is the detailed content of How to implement web page screenshots in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template