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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章