小編給大家分享一下PHP實現網頁截圖的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
PHP實現網頁截圖的方法:首先下載CutyCapt;然后解壓tar包;最后使用代碼“public function index(){echo '<pre>';echo date('Y-m-d H:m:s')...}”實現截圖即可。

如果需要將一個網址的內容做成視圖圖片輸出,則需要輔助工具
CutyCapt來實現這一需求
CutyCapt.exe 不需要再點擊安裝 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>';
}注意(如果直接命令行執行 $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、緣由鏈接
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實現網頁截圖的方法的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。