您的位置:首页 > 编程语言 > PHP开发

PHP使用phantomjs导出网址的图片,并使用proc_open输出到浏览器

2017-01-22 11:46 531 查看

1. phantomjs部分,保存为capture.js

用法:

phantomjs export.js http://www.bugshoot.cn png 5000 225*1364


"use strict";
var page = require('webpage').create(),
system = require('system'),
address, output, size, pageWidth, pageHeight;

if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage:\tphantomjs capture.js URL filename [paperwidth*paperheight] [cookies] [zoom]');
console.log('Cookies: key=value;key=value')
console.log('Eg1:\n\t// 生成图片为 ifmicro.png, 并且大小为宽1820px, 高1920px')
console.log('\tphantomjs capture.js http://www.bugshoot.cn/ ifmicro.png 1920px*1920px');
console.log('Eg2:\n\t// 生成图片为 ifmicro.jpg, 并且大小为宽1820px, 高1920*3/4px')
console.log('\tphantomjs capture.js http://www.bugshoot.cn/ ifmicro.png 1920px');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 600, height: 600 };

if (system.args.length > 3 && system.args[3].substr(-2) === "px") {
size = system.args[3].split('*');
if (size.length === 2) {
pageWidth = parseInt(size[0], 10);
pageHeight = parseInt(size[1], 10);
page.viewportSize = { width: pageWidth, height: pageHeight };
page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight };
} else {
console.log("size:", system.args[3]);
pageWidth = parseInt(system.args[3], 10);
pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any
console.log ("pageHeight:",pageHeight);
page.viewportSize = { width: pageWidth, height: pageHeight };
}
}

if (system.args.length > 4) {
var cookiesArr = system.args[4].split(";");
var cookiesJson = {};
for (var i=0; i<cookiesArr.length; i++) {
var tempCookie = cookiesArr[i].split('=');
cookiesJson[tempCookie[0].trim()] = tempCookie[1].trim()
phantom.addCookie({
name: tempCookie[0].trim(),
value: tempCookie[1].trim(),
domain: 'www.bugshoot.cn'
})
}
console.log(phantom.cookies)
}

if (system.args.length > 5) {
page.zoomFactor = system.args[5];
}

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();
}, 2500);
}
});
}


2.PHP部分。

思路是执行上述的shell操作,然后把接收到的数据作为PNG文件返回给浏览器。这里使用
proc_open
函数,相比
system``exec
等函数,优点是输入、输出、错误流是分开的。代码:

set_time_limit(0);
#关掉缓存,否则返回的内容会先打内存,大小会受脚本运行内存限制
ob_implicit_flush(true);
ob_end_flush();
header('Content-Disposition:  attachment;  filename=summary.png'  );
#header('Content-Length:' . $api_result['detail']['size']);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');

$command = 'phantomjs export.js '. \Yii::$app->params['CALLBACKURL'] .'export/post-data?md5=' . $md5 . '/#/'. $url.' png 5000 ' . $height .'*' . $width;
$descriptorSpec = array(
0 => array("pipe", "r"),#shell标准输入流
1 => array("pipe", "w"),#标准输出流
2 => array("pipe", "w")#错误流
);
$process = proc_open($command, $descriptorSpec, $pipes);
$flag = 1;
$text = '';
if (is_resource($process)) {
while (!feof($pipes[1])) {
if($flag)
{
fgets($pipes[1],strlen('data:image/png;base64,')+1);#去掉头
$flag = 0;
}
else{
$text .=  fgets($pipes[1], 1024);
}
}
echo base64_decode($text);#由于phantomjs直接输出png binary格式的api没有实现,需要从base64转换
fclose($pipes[1]);
proc_close($process);
}
exit;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php phantomjs proc-open