in reply to gnuplot output to browser

It appears that in the first example you print $img which is the output of the gnuplot command. In the second you are trying to print the filehandle $station.

You could choose to put the commands in a file and capture the output (as you did in the first case) or do the commands in the program and capture the output (with >somefile in the open) or you could print the header with your script, provide the plot commands in your script and let gnuplot write to the standard output.

HTH, --traveler