新聞中心
這篇文章主要講解了Java利用Phantomjs實現(xiàn)生成圖片的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
今天,給大家分享一個Java后端利用Phantomjs實現(xiàn)生成圖片的功能,同學(xué)們使用的時候,可以參考下!
PhantomJS簡介
首先,什么是PhantomJS?
根據(jù)官網(wǎng)介紹:
PhantomJS is a command-line tool. -- 其實就是一個命令行工具。
PhantomJS的下載地址:
Windows:phantomjs-2.1.1-windows.zip
Linux:phantomjs-2.1.1-linux-x86_64.tar.bz2;phantomjs-2.1.1-linux-i686.tar.bz2
MacOS:phantomjs-2.1.1-macosx.zip
下載下來后,我們看到bin目錄下就是可執(zhí)行文件phantomjs.exe
,我們可以將它配置到環(huán)境變量中,方便命令使用!
還有一個examples目錄,它下面是很多js樣例,關(guān)于這些樣例作用,參考官網(wǎng)解釋,給大家做個簡單翻譯:
1. Basic examples
- arguments.js:顯示傳遞給腳本的參數(shù)
- countdown.js:打印10秒倒計時
- echoToFile.js:將命令行參數(shù)寫入文件
- fibo.js:列出了斐波那契數(shù)列中的前幾個數(shù)字
- hello.js:顯示著名消息
- module.js:并universe.js演示模塊系統(tǒng)的使用
- outputEncoding.js:顯示各種編碼的字符串
- printenv.js:顯示系統(tǒng)的環(huán)境變量
- scandir.js:列出目錄及其子目錄中的所有文件
- sleepsort.js:對整數(shù)進行排序并根據(jù)其值延遲顯示
- version.js:打印出PhantomJS版本號
- page_events.js:打印出頁面事件觸發(fā):有助于更好地掌握page.on*回調(diào)
2. Rendering/rasterization
- colorwheel.js:使用HTML5畫布創(chuàng)建色輪
- rasterize.js:將網(wǎng)頁光柵化為圖像或PDF
- render_multi_url.js:將多個網(wǎng)頁渲染為圖像
3. Page automation
- injectme.js:將自身注入到網(wǎng)頁上下文中
- phantomwebintro.js:使用jQuery從phantomjs.org讀取.version元素文本
- unrandomize.js:在頁面初始化時修改全局對象
- waitfor.js:等待直到測試條件為真或發(fā)生超時
4. Network
- detectniff.js:檢測網(wǎng)頁是否嗅探用戶代理
- loadspeed.js:計算網(wǎng)站的加載速度
- netlog.js:轉(zhuǎn)儲所有網(wǎng)絡(luò)請求和響應(yīng)
- netsniff.js:以HAR格式捕獲網(wǎng)絡(luò)流量
- post.js:將HTTP POST請求發(fā)送到測試服務(wù)器
- postserver.js:啟動Web服務(wù)器并向其發(fā)送HTTP POST請求
- server.js:啟動Web服務(wù)器并向其發(fā)送HTTP GET請求
- serverkeepalive.js:啟動Web服務(wù)器,以純文本格式回答
- simpleserver.js:啟動Web服務(wù)器,以HTML格式回答
5. Testing
- run-jasmine.js:運行基于Jasmine的測試
- run-qunit.js:運行基于QUnit的測試
6. Browser
- features.js:檢測瀏覽器功能使用modernizr.js
- useragent.js:更改瀏覽器的用戶代理屬性
今天,我們根據(jù)網(wǎng)頁URL生成圖片,使用的就是rasterize.js:將網(wǎng)頁光柵化為圖像或PDF。
了解rasterize.js
我們來看一下rasterize.js的內(nèi)容(源文件對size的處理有錯誤,這里已修正?。?/p>
"use strict"; var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]'); console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"'); console.log(' image (png/jpg output) examples: "1920px" entire page, window width 1920px'); console.log(' "800px*600px" window, clipped to 800x600'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; page.viewportSize = { width: 800, height: 200 }; if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { size = system.args[3].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: system.args[3], orientation: 'portrait', margin: '1cm' }; } else if (system.args.length > 3 && system.args[3].substr(-2) === "px") { size = system.args[3].split('*'); if (size.length === 2) { var pageWidth = parseInt(size[0].substr(0,size[0].indexOf("px")), 10); var pageHeight = parseInt(size[1].substr(0,size[1].indexOf("px")), 10); page.viewportSize = { width: pageWidth, height: pageHeight }; page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight }; } else { var pageWidth = parseInt(system.args[3].substr(0,system.args[3].indexOf("px")), 10); var pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any page.viewportSize = { width: pageWidth, height: pageHeight }; } } if (system.args.length > 4) { page.zoomFactor = system.args[4]; } page.open(address, function (status) { if (status !== 'success') { console.log('Unable to load the address!'); phantom.exit(1); } else { window.setTimeout(function () { page.render(output); phantom.exit(); }, 200); } }); }
當前名稱:Java利用Phantomjs實現(xiàn)生成圖片的方法-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/dojhhd.html