Last active
November 29, 2016 01:46
-
-
Save Lunchbox4K/a022b2d43d4beb7fa08b03f841829330 to your computer and use it in GitHub Desktop.
PHP Gnuplot Grapher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* MP-Works PHP Gnuplot Tool | |
* | |
* @author Mitchell Pell <[email protected]> | |
* @copyright (c) 2016 Mitchell Pell - https://www.mp-works.us | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 | |
*/ | |
//# Variables | |
//------------------------------------------------------------ | |
$page_content = ''; /*! Web Page Content */ | |
$page_title = 'PHP Gnuplot Grapher '; /*! Web Page Title */ | |
$img_path = ''; /*! Path to generated gnuplot image. */ | |
$script_path = ''; /*! Path to created gnuplot script. */ | |
$script = ''; /*! Current gnuplot script. */ | |
$outputstr = ''; /*! Output from exec call. */ | |
$output = ''; /*! */ | |
//# Load Form Data from POST | |
//------------------------------------------------------------ | |
//If the page being loaded was from a POST REQUEST_METHOD (meaning form information was submitted.) | |
if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
//generate random string | |
$temp_name_hash = bin2hex(mcrypt_create_iv(22, MCRYPT_DEV_URANDOM)); | |
//create the script and image paths for saving to based on the random string. | |
$script_path = './gnuplots/plot_' . $temp_name_hash . '.ps'; | |
$img_path = './gnuplots/plot_' . $temp_name_hashpath . '.png'; | |
//try to fetch the gnuplot_script textarea contents from the page form. | |
//*to-to* sanitize, and validate the information sent in from the textarea. | |
$script = $_POST["gnuplot_script"]; | |
//search for and remove 'noinvert', because it was causing gnuplot to throw an error. | |
//*to-do* look into fix to allow 'noinvert'. | |
$script = str_replace('noinvert','',$script); | |
//Save the script to a file (.ps) and execuse gnuplot on success. | |
if ( file_put_contents($script_path, $script) ){ | |
try{ | |
$outputstr .= exec('gnuplot '.$script_path.' > '.$img_path, $output); | |
}catch(\Exception $e){ | |
$outputstr .= $e->getMessage(); | |
} | |
//If the script file could not be written. | |
}else{ | |
//Unable to write | |
$page_content .='<P><font color="red">ERROR: Unable to write script file!</font>'; | |
} | |
}//End of $_SERVER["REQUEST_METHOD"] == "POST" | |
//# Load temp script if none was passed with POST | |
if (strlen($script) == 0){ | |
$script = file_get_contents('gnuplots/3d-surface-01.ps'); | |
}//End of form data loading from POST | |
//# HTML Page Header | |
//------------------------------------------------------------ | |
$page_content .=' | |
<!--| c 2016 Mitchell Pell |--> | |
<!DOCTYPE html><html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- Styles --> | |
<link rel="stylesheet" href="https://bootswatch.com/darkly/bootstrap.min.css"> | |
<link href="mpw_engine/tp/jq-linedtext/jquery-linedtextarea.css" type="text/css" rel="stylesheet" /> | |
<!-- Scripts --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | |
<!-- @see http://alan.blog-city.com/jquerylinedtextarea.htm --> | |
<script src="mpw_engine/tp/jq-linedtext/jquery-linedtextarea.js"></script> | |
<!-- DISABLED <script src="mpw_engine/tp/codepress/codepress.js" type="text/javascript"></script> --> | |
<title>'.$page_title.'</title> | |
</head>'; | |
//# HTML Page Body | |
//------------------------------------------------------------ | |
//# HTML Page Body - Bootstrap panel for body w/ title | |
$page_content .=' | |
<body> | |
<div class="container"> | |
<div class="panel panel-default"> | |
<div class="panel-heading"> | |
<h3>'.$page_title.'</h3> | |
</div> | |
<div class="panel-body"> | |
<a href="http://gnuplot.sourceforge.net/demo_5.0/", target="_blank">More Gnuplot Scripts at Sourceforge.</a>'; | |
//# HTML Page Body - Form | |
$page_content .= ' | |
<hr> | |
<tag autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/> | |
<form action="gnuplot.php" method="POST" role="form" class="form-horizontal"> | |
<div class="form-group"> | |
<label for="gnuplot_script">Gnuplot Script</label> | |
<textarea class="lined" id="gnuplot_script" name="gnuplot_script" rows="24" cols="120"> | |
'.$script.' | |
</textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form>'; | |
//# HTML Page Body - Form Errors | |
$page_content .= ' | |
<!-- <div class="alert alert-danger"> | |
<strong>Error!</strong> '.$outputstr.' | |
</div> -->'; | |
//# HTML Page Body - lined textarea script (enables the lined textarea in the form) | |
// @see http://alan.blog-city.com/jquerylinedtextarea.htm | |
$page_content .= ' | |
<script> | |
$(function() { | |
// Target all classed with ".lined" | |
$(".lined").linedtextarea( | |
{selectedLine: 1} | |
); | |
// Target a single one | |
$("#mytextarea").linedtextarea(); | |
}); | |
</script>'; | |
//# HTML Page Body - Link to directory with saved plots | |
$page_content .= ' | |
<br> | |
<a href="./gnuplots/" target="_blank">Previous Plots</a> | |
<hr>'; | |
//# HTML Page Body - Generated image | |
$page_content .= ' | |
<img class="img-responsive" src="'.$img_path.'" alt="Gnuplot Results Image"> | |
'; | |
//# HTML Page Body - End of bootstrap panel for body | |
$page_content .= ' | |
</div> | |
</div> | |
<sub>Powered by MP-Works Engine (a0.01) | |
<br><span class="glyphicon glyphicon-copyright-mark"></span> | |
2016 Mitchell Pell</sub> | |
</div> | |
'; | |
//# HTML Page Body - End of page body, | |
$page_content .=' | |
<a name="bottomOfPage"></a> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
<script> | |
window.scrollTo(0,document.body.scrollHeight); | |
</script> | |
</body> | |
</html> '; | |
//# Finally, Print the pages contents. | |
print($page_content); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ---------------------------------------------------------- | |
# Gnuplot Helper | |
# Script for Generating a Gnuplot Image (.png) | |
# from a Gnuplot Script (.ps) | |
# Only allow the script to be run by the user gnuplot | |
if [ "$USER" != "gnuplot" ] | |
then echo "error a" | |
exit | |
# Check for proper number of arguments. | |
elif [ "$#" -ne 2 ] | |
then echo "error b" | |
exit | |
fi | |
# Execute | |
gnuplot "$1" > "$2" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment