Link to home
Start Free TrialLog in
Avatar of madmax61
madmax61

asked on

Browser output of HTML tables plus gnuplot image on same page without use of external files

In a browser running a perl CGI-BIN program(Linux) I want to call another program (FORTRAN program running on Linux) to extract data which passes back the data (after a lot of mathematical processing) to the perl program, and the result being 2 HTML tables followed by a single gnuplot plot of curves all on one page of the browser.

I can do just the HTML tables output no worries.

I think I can do the curves on a page on its own without the use of creating files on the Linux server and linking back to it from the HTML. That is generating the plot straight to the browser using:
(sample of code below)
$process=open2(GP,GP,"/usr/bin/gnuplot");
print GP "set terminal png small color");
print GP "title ...
print GP ....size, border, logscale,xrange,yrange,xlabel,ylabel,xtics,grid etc etc

print $query->header('image/png');
# initiate the plot
print GP "plot '-' using 1:2 title \"1 year\" with linespoints lt 1 pt 1 ps 2, '-' using 1:2 title ...etc


The problem with this code above is I can ONLY plot the gnuplot curve on the browser page because of the' image/png' header telling the browser the following data hitting the browser is an image. That is I cannot put the HTML tables below or above the gnuplot.

Any ideas how I can get both HTML generated tables of the data, PLUS an image generated on the fly by gnuplot?

P.S. The gnuplot module within perl is not installed on this system (I believe), and not sure if they would either. Gnuplot is installed as a standalone appilcation on the unix server though.


Note I am trying to to all this without creating files on the Linux server and then linking to it in the HTML followed by necessary file cleanup, unless you can help me with creating random filenames, and cleaning up afterwards I suppose. This program may be called by many people on a high traffic site.

Cheers,
Max



 



ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of madmax61
madmax61

ASKER

Thankyou so far, looks promising.....

Here is a sample of the type of thing I am doing:

I can't get it to work, could you please give me a little more guidance in how to get it working.


#!/usr/bin/perl
use IPC::Open2;

$process = open2(OUT, GP, "/usr/bin/gnuplot");
print GP << "end_commands";
set term png
set title "Test 2"
plot sin(x)
end_commands
close GP;
$| = 1;
print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
print @pngdata;


From your link provided I need it to look similar to:

<IMG
   SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw
   AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz
   ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp
   a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl
   ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis
   F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH
   hhx4dbgYKAAA7"
   ALT="Larry">
 
I'm modifying these 3 lines essentially:

print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
print @pngdata;

to look like:

print "<img src=\"data:image/png;base64,",@pngdata,\">";

But I get screen garbage (eg text representing the image):

IT looks like:

<img src="data:image/png;base64,‰PNG

   
IHDR  €  à

Any ideas where I am going wrong?

Cheers,
Max
The program in full (which doesn't work) currently is:

#!/usr/bin/perl
use IPC::Open2;

#($returned,$ifdtable)=split('::::',`/web/cgi-bin/hydro/has/ifd_table_simple 23000 123000`);
#print $returned;
print "Content_Type: text/html\n\n";
print "<html>";
print "<head><title>testing 1 2 3<\/title><\/head>";
print "<body>";
print "blah blah blah";

$process = open2(OUT, GP, "/usr/bin/gnuplot");
print GP << "end_commands";
set term png
set title "Test 2"
plot sin(x)
end_commands
close GP;
$| = 1;
#print "Content_Type: image/png\n\n";
@pngdata = <OUT>;
#print @pngdata;
print "<img src=\"data:image/png;base64,",@pngdata,\">";

print "blah blah blah again";
print "<\/body>";
print "<\/html>";
I tried the sample from the site in a file, and that doesnt display a picture either:

<html>
<head><title>test</title></head>
<body>

testing

<IMG

SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfz

gFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeY

GejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5V

WzXyym7PHhhx4dbgYKAAA7"ALT="Larry">

testing 2

</body>
</html>
Some more googling uncovered this statement:

" It seems that IE6 does not support the RFC 2397 'data' scheme. Maybe it will
in IE7.
 Firefox and Opera support it. "

I tried the sample and it worked in mozilla, and doesnt work on my IE6. Still didnt get my own program working in mozilla but defeats the purpose if it won't work on IE6 and below. It isn't an acceptable solution to only work in version 7 of IE and above.

Cheers,
Max


Could you make one script generate the
Content_Type: text/html\n\n
<html>
<head><title>testing 1 2 3<\/title><\/head>
<body>
blah blah blah
<img src="otherscripttogenerateimage?datatogenerateimage">
and do the
Content_Type: image/png\n\n@pngdata
in the otherscript
What if you put the file handle into binary mode before you print the image?
Ozo:

 RE: <img src="otherscripttogenerateimage?datatogenerateimage">

Is this valid HTML, can I call a script like that? Also will it too be part of the same or similar RFC that won't support less than I.E. 7?

Mjcoyne:

Are you suggesting this line:
$process = open2(OUT, GP, "/usr/bin/gnuplot");
be given a binary mode for the open ?

How will this help, what are you trying to achieve there?
Sorry I don't understand where you are going with that.


p.s. Apologies for not getting back sooner, I have been on a course this week.

Regards and thanks,
Max

You earlier said "But I get screen garbage (eg text representing the image)".  This is usually the result of printing a binary file in ASCII mode.  I would put whatever filehandle produced the "screen garbage" into binary mode.

Why do you not want gnuplot to just create a file on the Linux server that you can link to via HTML?

I'm not sure how we got into base64...

I've decided I will go down the path of creating files on linux, and linking to it, and will need to run a file cleanup routine in cron.

Whilst I think the answers given will work under certain circumstances (eg certain browser levels) I don't feel it is suitable for me, so I will still award the points for the suggestions.

Cheers,
Max