1

I have a PHP file which renders a PNG image (see online, based on Google Charts example). I want to "execute" this file using cronjob, and use it to generate PNG image on my server every day.

Is this possible? I can run PHP/Bash using cronjob. I tried running PHP script which executed the HTML using PHP's eval(), but how would I pipe the output to PNG file?

google.charts.load("current", {
  packages: ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {

  var data = google.visualization.arrayToDataTable([
    ['Element', 'Density', {
      role: 'style'
    }],
    ['Copper', 8.94, '#b87333', ],
    ['Silver', 10.49, 'silver'],
    ['Gold', 19.30, 'gold'],
    ['Platinum', 21.45, 'color: #e5e4e2']
  ]);

  var options = {
    title: "Density of Precious Metals, in g/cm^3",
    bar: {
      groupWidth: '95%'
    },
    legend: 'none',
  };

  var chart_div = document.getElementById('chart_div');
  var chart = new google.visualization.ColumnChart(chart_div);

  // Wait for the chart to finish drawing before calling the getImageURI() method.
  google.visualization.events.addListener(chart, 'ready', function() {
    chart_div.innerHTML = '<img src="' + chart.getImageURI() + '">';
    console.log(chart_div.innerHTML);
  });

  chart.draw(data, options);

}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id='chart_div'></div>
showdev
  • 28,454
  • 37
  • 55
  • 73
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
  • You're generating an image with JavaScript, you want to save it as an image file, and you want to do this as a cron job. Is that right? – showdev Mar 14 '19 at 20:52
  • 3
    If you insist on using javascript to generate the visualization, you should consider either NodeJS or running a headless browser to execute your client side JS. You can't use PHP's `eval()` function to execute javascript the way you are trying to do it. We need more clarity -- are you trying to use PHP or are you trying to use Javascript? – Jeremy Harris Mar 14 '19 at 21:05
  • 2
    You'll [need a browser to run the JavaScript](https://stackoverflow.com/questions/15904071/), or you can use something like [Node.js to run it server-side](https://stackoverflow.com/questions/9249876/). You could use a [headless browser](https://stackoverflow.com/a/40532287/924299), then [pass the base64 string to a PHP script](https://stackoverflow.com/questions/12923145/) that [writes it to an image file](https://stackoverflow.com/questions/11511511/). See [generate server-side google charts as images](https://stackoverflow.com/questions/24024616/). – showdev Mar 14 '19 at 21:09
  • PHP would be the best option. I don't want to use NodeJS or some other heavy solution. What is I use [this PHP treemap generator](https://codeagent.github.io/treemap/), which allows rendering a PNG. How do I then run this using cronjob so the image would be saved on server? I know I can manully visit this PHP and get an image on screen, but I need to do it using cron. – c0dehunter Mar 15 '19 at 05:13
  • You can execute PHP with cron. As you mentioned, Treemap seems to provide a [representation format for raw image data](https://github.com/codeagent/treemap#user-content-formats-of-representation); also see [map format: image](https://codeagent.github.io/treemap/#image). Theoretically, you could use that output and [write an image file to disk](http://php.net/manual/en/function.imagepng.php), but I haven't tried it. – showdev Mar 15 '19 at 08:35

1 Answers1

0

I would prefer to use other PHP libs to do that.

It's weird idea but... you can use

firefox -headless http://localhost/script.html

and pass chart.getImageURI() to PHP script through ajax, decode it and save. Then run shell_exec to kill all firefox process(I guess u will need to use sudo).

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459