var express = require('express');var router = express.Router();var phantom = require('phantom');var web2img = (url, res, dataType, width, height) => { phantom.create().then(function (ph) { ph.createPage().then((page) => { var settings = { // operation: "POST", encoding: "utf8" }; page.open(url, settings).then((status) => { console.log(status); var fmt = 'JPEG'; if (status == 'success') { page.property('viewportSize', {width: width || 540, height: height || 960}); page.renderBase64(fmt).then((data) => { if (dataType == 0) { res.json({code: 1, data: "data:image/png;base64," + data}); } else if (dataType == 1) { res.send("data:image/png;base64," + data); } else if (dataType == 2) {//檔案輸出 var imageBuffer = new Buffer(data, 'base64'); res.send(imageBuffer); } }); } else { res.json({code: 0, msg: '生成圖片異常'}); } ph.exit(); }); }); });};router.all('/web2img', function (req, res, next) { let url = req.query.url; let width = req.query.width; let height = req.query.height; let dataType = req.query.dataType || 0; web2img(url, res, dataType, width, height);});module.exports = router;
最新評論