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服务器端导出Echarts图片
- php使用GD图像库绘制输出图像出现乱码问题和图片上输出中文出现乱码问题解决方法。
- 使用phantomJs服务器端导出Echarts图片
- php读取图片内容并输出到浏览器的实现代码
- php 查询数据库所要输出的图片无法在浏览器上显示
- php使用phpexcel生成excel包含图片并导出
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
- 命令行cli使用php的exec可以正常,浏览器访问无输出解决方案
- PHP使用header()输出图片缓存实例
- PHP实现浏览器中直接输出图片的方法示例
- php读取图片内容并输出到浏览器
- php导出csv数据在浏览器中输出提供下载或保存到文件的示例
- Highcharts使用phantomjs导出图片
- PHP中向浏览器输出图片
- php获取本地图片直接输出浏览器,file_get_contents
- PHP用proc_open新建子进程使用管道通信非阻塞执行PHP脚本
- PHP使用header()输出图片缓存实例
- PHP用GD库绘制图片,制作验证码浏览器不能输出
- php读取图片内容并输出到浏览器的实现代码