我想用phantomJs获取Google图表的image / png; base64(服务器端)
$base64 = exec('phantomjs myscript.js');
header("Content-type: image/png");
echo base64_decode( $base64 );
myscript.js:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log( msg );
phantom.exit();
};
page.open('http://getpngchart.dev/chart.php');
chart.php:
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable( [['Task', 'Quantity'], ['Critical', 35], ['Routine', 6], ['Adjust', 2]] );
var options = {colors: ['#E34036','#F7A500','#FF0097'], legend: { position: "none" }};
var chart = new google.visualization.PieChart( document.getElementById('chart_div') );
google.visualization.events.addListener(chart, 'ready', function () {
console.log( chart.getImageURI() );
});
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width:250px; height:250px;"></div>
</body>
</html>
当我使用Chrome启动chart.php时,我得到了预期的base64字符串
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAYAAACI7Fo9AAAgAElEQVR4Xu2dCXwURfbHX889kzshCSHgKuB6gIp4LIgicrgiCyogyCEquq7ihbgerCvoKvtfr9VVBEQ...AiNzE1kBBp19gBWQQAEGXQIjcxNZAQadfYAVkEABBl0CI3MTWQEGnX2AFZBAAQZdAiNzE1mB/wedEj8SPZNwdQAAAABJRU5ErkJggg==
但是当我使用终端(#phantomjs myscript.js)时,我得到了一个带有很多AAAA的错误的base64 ......:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAewAAAHkCAYAAAD8eRwNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEwAACxMBAJqcGAAAIABJREFUeAEAwYI+fQH/////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
我的目标是使用Chrome和phantomJS获得相同的base64
答案 0 :(得分:0)
这个脚本运行正常。但是需要在@ line2
上对数据流进行一些小调整$base64 = str_replace(' ','+',$base64);
//Remove identifier string from begining of data.
$base64 = str_replace('data:image/png;base64,', '', $base64);