|
From: Richard H. <r.h...@rl...> - 2008-01-11 14:24:45
|
jimineep wrote:
>
> I've just done it in perl/cgi so I don't see why php couldnt do it!
>
> I did it by saving off the data to a temporary .dat file, then running
> gnuplot from my script using this file, saving the output to a png file
> (jpeg might be possible...probably something like set term jpeg) and then
> getting my script to write an html output displaying this .png file. Im no
> expert in perl or gnuplot but it wasnt too tricky.
>
> A question for anyone who might know........is it possible for me to by
> pass saving off the temporary file and instead keeping a 2d array in
> memory and sending it straight to gnuplot
>
yes; one method is to write and read from stdin/stdout.
the code below (untested) uses this technique.
however, you should read up on piping to stdin reading from
stdout with perl: there are a number of issues to consider.
In addition, one must give consideration to security when
deploying gnuplot on a webserver.
hope this helps,
r,
--begin code example--
#
# call gnuplot into existence and render the commands, returning a binary blob
# from gnuplot. we use bidirectional communication.
# this has a problem in that we can't garentee to receive a result from our process
# in a timely manner.
# In short, if something goes wrong with gnuplot input, we will have to wait till
# apache kills the session. hopefully.
#
my $gnuplotCommands = "set term png; plot sin(x)";
#&errmsg("commands = '$gnuplotCommands'");
my $pid = open2(*Reader, *Writer, " gnuplot " ) or die "could not open $gnuplot: $!";
select *Writer;
$| = 1; # flush all writes down the pipe.
print Writer $gnuplotCommands or die "Could not write data to $gnuplot: $!";
close Writer or die "Ploblem closing $gnuplot, status=$?";
select STDOUT;
binmode(Reader);
my $buffer = "";
my $binaryPic = "";
while (read Reader, $buffer, 1024) {
$binaryPic .= $buffer;
}
close Reader or die "Problem closing $gnuplot, status=$?";
print 'Content-type: image/png; name"testfile"\n';
print 'Content-Disposition: filename="testfile.png';
print "\n\n";
print $binaryPic;
--end code example--
|