基於linnux+phantomjs實現產生圖片格式的網頁快照,
安裝擴充:
(1)下面是我在linux上的安裝過程,如果沒有安裝git請先yum install git
安裝casperjs
複製代碼 代碼如下:
cd /
git clone git://github.com/n1k0/casperjs.git
cd casperjs
ln -sf /casperjs/bin/casperjs /usr/local/bin/casperjs //可以忽略 實際執行中php是執行 /casperjs/bin/casperjs
(2)安裝phantomjs,下載地址:http://phantomjs.org/download.html
下載後操作很簡單,直接把解壓好的\bin\phantomjs移動到\usr\local\bin\phantomjs就可以了。\
測試phantomjs --version 有結果不報錯,說明安裝OK
(3)安裝字型
1. 首先獲得一套“微軟雅黑”字型庫(Google一下一大把),包含兩個檔案msyh.ttf(普通)、msyhbd.ttf(加粗);
2. 在/usr/share/fonts目錄下建立一個子目錄,例如win,命令如下:
複製代碼 代碼如下:
# mkdir /usr/share/fonts/win
3. 將msyh.ttf和msyhbd.ttf複製到該目錄下,例如這兩個檔案放在/root/Desktop下,使用命令:
複製代碼 代碼如下:
# cd /root/Desktop
# cp msyh.ttf msyhbd.ttf /usr/share/fonts/win/
4. 建立字型索引資訊,更新字型緩衝:
複製代碼 代碼如下:
# cd /usr/share/fonts/win
# mkfontscale (如果提示 mkfontscale: command not found,需自行安裝 # yum install mkfontscale )
# mkfontdir
# fc-cache (如果提示 fc-cache: command not found,則需要安裝# yum install fontconfig )
至此,字型已經安裝完畢!
<?php if (isset($_GET['url'])) { set_time_limit(0); $url = trim($_GET['url']); $filePath = md5($url).'.png'; if (is_file($filePath)) { exit($filePath); } //如果不加這句就會報錯“Fatal: [Errno 2] No such file or directory; did you install phantomjs?”,詳情參考http://mengkang.net/87.html putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs"); $command = "phantomjs phantomjs.js {$url} {$filePath}"; @exec($command); exit($filePath); } ?> 快照產生
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(); });
以上所述就是本文的全部內容了,希望大家能夠喜歡。
http://www.bkjia.com/PHPjc/984621.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/984621.htmlTechArticle基於linnux+phantomjs實現產生圖片格式的網頁快照, 安裝擴充: (1)下面是我在linux上的安裝過程,如果沒有安裝git請先yum install git 安裝casperjs...