Gnuplot system call from php to generate a graph?

So, I use the php program to read the file, make some changes, and then write it to a new file. After that, I call gnuplot using the system call:

system('cat sarx.conf | /usr/bin/gnuplot'); 

sarx.conf has gnuplot commands for generating a chart. The problem is that if you run my php from the command line (on a Linux server), it generates an image and saves it to disk. But when I do the same, running php in my browser, it generates an image and tries to spit it out in the browser without saving it to disk.

Things I tried:

  • I might have had problems with permission settings, but that didn't help.

  • I also hardcoded the path where I want the image to be in sarx.conf . It didn’t help either.

  • I also tried looking for it in the tmp directory --- no luck !!

Does anyone have any ideas on how I can make this work? I need to save this image to disk so that my site can capture it in order to show the plot later. Is there any php stuff that can capture an image and burn it to disk?

+4
source share
1 answer

Gnuplot has a great interface for LGPL licensing: http://www.liuyi1.com/PHP-GNUPlot/

Here's how you could do something like this:

 $my_file = tempnam(); $handle = popen('gnuplot', 'w'); fwrite($this->ph, "Run some gnuplot commands here\n"); fwrite($this->ph, "set term png\n"); fwrite($this->ph, "set output ".$my_file."\n"); fwrite($this->ph, "replot\n"); flush($handle); pclose($handle); header('Content-Length: '.filesize($my_file)); header('Content-Type: image/png'); print file_get_contents($my_file); unlink($my_file); 
+1
source

All Articles