/deprecated/includes/eos.class.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2. /** 
  3. * Equation Operating System Classes. 
  4. *  
  5. * This class was created for the safe parsing of mathematical equations 
  6. * in PHP. There is a need for a way to successfully parse equations 
  7. * in PHP that do NOT require the use of `eval`. `eval` at its core 
  8. * opens the system using it to so many security vulnerabilities it is oft 
  9. * suggested /never/ to use it, and for good reason. This class set will 
  10. * successfully take an equation, parse it, and provide solutions to the 
  11. * developer. It is a safe way to evaluate expressions without putting 
  12. * the system at risk. 
  13. * 
  14. * 2013/04 UPDATE: 
  15. * - Moved to native class functions for PHP5 
  16. * - Removed deprecated `eregi` calls to `preg_match` 
  17. * - Updated to PHPDoc comment syntax 
  18. * - Added Exception throwing instead of silent exits 
  19. * - Added additional variable prefix of '$', '&' is still allowed as well 
  20. * - Fixed small implied multiplication problem 
  21. * 
  22. * TODO: 
  23. * - Add factorial support. (ie 5! = 120) 
  24. * 
  25. * @author Jon Lawrence <jlawrence11@gmail.com> 
  26. * @copyright Copyright *2005-2013, Jon Lawrence 
  27. * @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License 
  28. * @package EOS 
  29. * @version 2.0 
  30. */ 
  31.  
  32. //The following are defines for thrown exceptions 
  33.  
  34. /** 
  35. * No matching Open/Close pair 
  36. */ 
  37. define('EQEOS_E_NO_SET', 5500); 
  38. /** 
  39. * Division by 0 
  40. */ 
  41. define('EQEOS_E_DIV_ZERO', 5501); 
  42. /** 
  43. * No Equation 
  44. */ 
  45. define('EQEOS_E_NO_EQ', 5502); 
  46. /** 
  47. * No variable replacement available 
  48. */ 
  49. define('EQEOS_E_NO_VAR', 5503); 
  50.  
  51. if(!defined('DEBUG')) 
  52. define('DEBUG', false); 
  53.  
  54. //We use a stack class so we don't have to keep track of indices for an array 
  55. // May eventually update to use `array_pop()` `end()` and `array_push()` instead 
  56. // of this class. 
  57. require_once "stack.class.php"; 
  58.  
  59.  
  60. /** 
  61. * Equation Operating System (EOS) Parser 
  62. * 
  63. * An EOS that can safely parse equations from unknown sources returning 
  64. * the calculated value of it. Can also handle solving equations with 
  65. * variables, if the variables are defined (useful for the Graph creation 
  66. * that the second and extended class in this file provides. {@see eqGraph}) 
  67. * This class was created for PHP4 in 2005, updated to fully PHP5 in 2013. 
  68. *  
  69. * @author Jon Lawrence <jlawrence11@gmail.com> 
  70. * @copyright Copyright *2005-2013, Jon Lawrence 
  71. * @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License 
  72. * @package Math 
  73. * @subpackage EOS 
  74. * @version 2.0 
  75. */ 
  76. class eqEOS { 
  77. /**#@+ 
  78. *Private variables 
  79. */ 
  80. private $postFix; 
  81. private $inFix; 
  82. /**#@-*/ 
  83. /**#@+ 
  84. * Protected variables 
  85. */ 
  86. //What are opening and closing selectors 
  87. protected $SEP = array('open' => array('(', '['), 'close' => array(')', ']')); 
  88. //Top presedence following operator - not in use 
  89. protected $SGL = array('!'); 
  90. //Order of operations arrays follow 
  91. protected $ST = array('^'); 
  92. protected $ST1 = array('/', '*', '%'); 
  93. protected $ST2 = array('+', '-'); 
  94. //Allowed functions 
  95. protected $FNC = array('sin', 'cos', 'tan', 'csc', 'sec', 'cot'); 
  96. /**#@-*/ 
  97. /** 
  98. * Construct method 
  99. * 
  100. * Will initiate the class. If variable given, will assign to 
  101. * internal variable to solve with this::solveIF() without needing 
  102. * additional input. Initializing with a variable is not suggested. 
  103. * 
  104. * @see eqEOS::solveIF() 
  105. * @param String $inFix Standard format equation 
  106. */ 
  107. public function __construct($inFix = null) { 
  108. $this->inFix = (isset($inFix)) ? $inFix : null; 
  109. $this->postFix = array(); 
  110.  
  111. /** 
  112. * Check Infix for opening closing pair matches. 
  113. * 
  114. * This function is meant to solely check to make sure every opening 
  115. * statement has a matching closing one, and throws an exception if 
  116. * it doesn't. 
  117. * 
  118. * @param String $infix Equation to check 
  119. * @throws Exception if malformed. 
  120. * @return Bool true if passes - throws an exception if not. 
  121. */ 
  122. private function checkInfix($infix) { 
  123. if(trim($infix) == "") { 
  124. throw new Exception("No Equation given", EQEOS_E_NO_EQ); 
  125. return false; 
  126. //Make sure we have the same number of '(' as we do ')' 
  127. // and the same # of '[' as we do ']' 
  128. if(substr_count($infix, '(') != substr_count($infix, ')')) { 
  129. throw new Exception("Mismatched parenthesis in '{$infix}'", EQEOS_E_NO_SET); 
  130. return false; 
  131. } elseif(substr_count($infix, '[') != substr_count($infix, ']')) { 
  132. throw new Exception("Mismatched brackets in '{$infix}'", EQEOS_E_NO_SET); 
  133. return false; 
  134. $this->inFix = $infix; 
  135. return true; 
  136.  
  137. /** 
  138. * Infix to Postfix 
  139. * 
  140. * Converts an infix (standard) equation to postfix (RPN) notation. 
  141. * Sets the internal variable $this->postFix for the eqEOS::solvePF() 
  142. * function to use. 
  143. * 
  144. * @link http://en.wikipedia.org/wiki/Infix_notation Infix Notation 
  145. * @link http://en.wikipedia.org/wiki/Reverse_Polish_notation Reverse Polish Notation 
  146. * @param String $infix A standard notation equation 
  147. * @return Array Fully formed RPN Stack 
  148. */ 
  149. public function in2post($infix = null) { 
  150. // if an equation was not passed, use the one that was passed in the constructor 
  151. $infix = (isset($infix)) ? $infix : $this->inFix; 
  152.  
  153. //check to make sure 'valid' equation 
  154. $this->checkInfix($infix); 
  155. $pf = array(); 
  156. $ops = new phpStack(); 
  157. $vars = new phpStack(); 
  158.  
  159. // remove all white-space 
  160. preg_replace("/\s/", "", $infix); 
  161.  
  162. // Create postfix array index 
  163. $pfIndex = 0; 
  164.  
  165. //what was the last character? (useful for decerning between a sign for negation and subtraction) 
  166. $lChar = ''; 
  167.  
  168. //loop through all the characters and start doing stuff ^^ 
  169. for($i=0;$i<strlen($infix);$i++) { 
  170. // pull out 1 character from the string 
  171. $chr = substr($infix, $i, 1); 
  172.  
  173. // if the character is numerical 
  174. if(preg_match('/[0-9.]/i', $chr)) { 
  175. // if the previous character was not a '-' or a number 
  176. if((!preg_match('/[0-9.]/i', $lChar) && ($lChar != "")) && (@$pf[$pfIndex]!="-")) 
  177. $pfIndex++; // increase the index so as not to overlap anything 
  178. // Add the number character to the array 
  179. @$pf[$pfIndex] .= $chr; 
  180. // If the character opens a set e.g. '(' or '[' 
  181. elseif(in_array($chr, $this->SEP['open'])) { 
  182. // if the last character was a number, place an assumed '*' on the stack 
  183. if(preg_match('/[0-9.]/i', $lChar)) 
  184. $ops->push('*'); 
  185.  
  186. $ops->push($chr); 
  187. // if the character closes a set e.g. ')' or ']' 
  188. elseif(in_array($chr, $this->SEP['close'])) { 
  189. // find what set it was i.e. matches ')' with '(' or ']' with '[' 
  190. $key = array_search($chr, $this->SEP['close']); 
  191. // while the operator on the stack isn't the matching pair...pop it off 
  192. while($ops->peek() != $this->SEP['open'][$key]) { 
  193. $nchr = $ops->pop(); 
  194. if($nchr) 
  195. $pf[++$pfIndex] = $nchr; 
  196. else { 
  197. throw new Exception("Error while searching for '". $this->SEP['open'][$key] ."' in '{$infix}'.", EQEOS_E_NO_SET); 
  198. return false; 
  199. $ops->pop(); 
  200. // If a special operator that has precedence over everything else 
  201. elseif(in_array($chr, $this->ST)) { 
  202. $ops->push($chr); 
  203. $pfIndex++; 
  204. // Any other operator other than '+' and '-' 
  205. elseif(in_array($chr, $this->ST1)) { 
  206. while(in_array($ops->peek(), $this->ST1) || in_array($ops->peek(), $this->ST)) 
  207. $pf[++$pfIndex] = $ops->pop(); 
  208.  
  209. $ops->push($chr); 
  210. $pfIndex++; 
  211. // if a '+' or '-' 
  212. elseif(in_array($chr, $this->ST2)) { 
  213. // if it is a '-' and the character before it was an operator or nothingness (e.g. it negates a number) 
  214. if((in_array($lChar, array_merge($this->ST1, $this->ST2, $this->ST, $this->SEP['open'])) || $lChar=="") && $chr=="-") { 
  215. // increase the index because there is no reason that it shouldn't.. 
  216. $pfIndex++; 
  217. $pf[$pfIndex] = $chr;  
  218. // Otherwise it will function like a normal operator 
  219. else { 
  220. while(in_array($ops->peek(), array_merge($this->ST1, $this->ST2, $this->ST))) 
  221. $pf[++$pfIndex] = $ops->pop(); 
  222. $ops->push($chr); 
  223. $pfIndex++; 
  224. // make sure we record this character to be refered to by the next one 
  225. $lChar = $chr; 
  226. // if there is anything on the stack after we are done...add it to the back of the RPN array 
  227. while(($tmp = $ops->pop()) !== false) 
  228. $pf[++$pfIndex] = $tmp; 
  229.  
  230. // re-index the array at 0 
  231. $pf = array_values($pf); 
  232.  
  233. // set the private variable for later use if needed 
  234. $this->postFix = $pf; 
  235.  
  236. // return the RPN array in case developer wants to use it fro some insane reason (bug testing ;] 
  237. return $pf; 
  238. } //end function in2post 
  239.  
  240. /** 
  241. * Solve Postfix (RPN) 
  242. *  
  243. * This function will solve a RPN array. Default action is to solve 
  244. * the RPN array stored in the class from eqEOS::in2post(), can take 
  245. * an array input to solve as well, though default action is prefered. 
  246. * 
  247. * @link http://en.wikipedia.org/wiki/Reverse_Polish_notation Postix Notation 
  248. * @param Array $pfArray RPN formatted array. Optional. 
  249. * @return Float Result of the operation. 
  250. */ 
  251. public function solvePF($pfArray = null) { 
  252. // if no RPN array is passed - use the one stored in the private var 
  253. $pf = (!is_array($pfArray)) ? $this->postFix : $pfArray; 
  254.  
  255. // create our temporary function variables 
  256. $temp = array(); 
  257. $tot = 0; 
  258. $hold = 0; 
  259.  
  260. // Loop through each number/operator  
  261. for($i=0;$i<count($pf); $i++) { 
  262. // If the string isn't an operator, add it to the temp var as a holding place 
  263. if(!in_array($pf[$i], array_merge($this->ST, $this->ST1, $this->ST2))) { 
  264. $temp[$hold++] = $pf[$i]; 
  265. // ...Otherwise perform the operator on the last two numbers  
  266. else { 
  267. switch ($pf[$i]) { 
  268. case '+': 
  269. @$temp[$hold-2] = $temp[$hold-2] + $temp[$hold-1]; 
  270. break; 
  271. case '-': 
  272. @$temp[$hold-2] = $temp[$hold-2] - $temp[$hold-1]; 
  273. break; 
  274. case '*': 
  275. @$temp[$hold-2] = $temp[$hold-2] * $temp[$hold-1]; 
  276. break; 
  277. case '/': 
  278. if($temp[$hold-1] == 0) { 
  279. throw new Exception("Division by 0 on: '{$temp[$hold-2]} / {$temp[$hold-1]}' in {$this->inFix}", EQEOS_E_DIV_ZERO); 
  280. return false; 
  281. @$temp[$hold-2] = $temp[$hold-2] / $temp[$hold-1]; 
  282. break; 
  283. case '^': 
  284. @$temp[$hold-2] = pow($temp[$hold-2], $temp[$hold-1]); 
  285. break; 
  286. case '%': 
  287. if($temp[$hold-1] == 0) { 
  288. throw new Exception("Division by 0 on: '{$temp[$hold-2]} % {$temp[$hold-1]}' in {$this->inFix}", EQEOS_E_DIV_ZERO); 
  289. return false; 
  290. @$temp[$hold-2] = bcmod($temp[$hold-2], $temp[$hold-1]); 
  291. break; 
  292. // Decrease the hold var to one above where the last number is  
  293. $hold = $hold-1; 
  294. // return the last number in the array  
  295. return $temp[$hold-1]; 
  296.  
  297. } //end function solvePF 
  298.  
  299.  
  300. /** 
  301. * Solve Infix (Standard) Notation Equation 
  302. * 
  303. * Will take a standard equation with optional variables and solve it. Variables 
  304. * must begin with '&' will expand to allow variables to begin with '$' (TODO) 
  305. * The variable array must be in the format of 'variable' => value. If 
  306. * variable array is scalar (ie 5), all variables will be replaced with it. 
  307. * 
  308. * @param String $infix Standard Equation to solve 
  309. * @param String|Array $vArray Variable replacement 
  310. * @return Float Solved equation 
  311. */ 
  312. function solveIF($infix, $vArray = null) { 
  313. $infix = ($infix != "") ? $infix : $this->inFix; 
  314.  
  315. //Check to make sure a 'valid' expression 
  316. $this->checkInfix($infix); 
  317.  
  318. $ops = new phpStack(); 
  319. $vars = new phpStack(); 
  320.  
  321. //remove all white-space 
  322. preg_replace("/\s/", "", $infix); 
  323. if(DEBUG) 
  324. $hand=fopen("eq.txt", "a"); 
  325.  
  326. //Find all the variables that were passed and replaces them 
  327. while((preg_match('/(.) {0, 1}[&$]([a-zA-Z]+)(.) {0, 1}/', $infix, $match)) != 0) { 
  328.  
  329. //remove notices by defining if undefined. 
  330. if(!isset($match[3])) { 
  331. $match[3] = ""; 
  332.  
  333. if(DEBUG) 
  334. fwrite($hand, "{$match[1]} || {$match[3]}\n"); 
  335. // Ensure that the variable has an operator or something of that sort in front and back - if it doesn't, add an implied '*' 
  336. if((!in_array($match[1], array_merge($this->ST, $this->ST1, $this->ST2, $this->SEP['open'])) && $match[1] != "") || is_numeric($match[1])) //$this->SEP['close'] removed 
  337. $front = "*"; 
  338. else 
  339. $front = ""; 
  340.  
  341. if((!in_array($match[3], array_merge($this->ST, $this->ST1, $this->ST2, $this->SEP['close'])) && $match[3] != "") || is_numeric($match[3])) //$this->SEP['open'] removed 
  342. $back = "*"; 
  343. else 
  344. $back = ""; 
  345.  
  346. //Make sure that the variable does have a replacement 
  347. if(!isset($vArray[$match[2]]) && (!is_array($vArray != "") && !is_numeric($vArray))) { 
  348. throw new Exception("Variable replacement does not exist for '". substr($match[0], 1, -1) ."' in {$this->inFix}", EQEOS_E_NO_VAR); 
  349. return false; 
  350. } elseif(!isset($vArray[$match[2]]) && (!is_array($vArray != "") && is_numeric($vArray))) { 
  351. $infix = str_replace($match[0], $match[1] . $front. $vArray. $back . $match[3], $infix); 
  352. } elseif(isset($vArray[$match[2]])) { 
  353. $infix = str_replace($match[0], $match[1] . $front. $vArray[$match[2]]. $back . $match[3], $infix); 
  354.  
  355. if(DEBUG) 
  356. fwrite($hand, "$infix\n"); 
  357.  
  358. // Finds all the 'functions' within the equation and calculates them  
  359. // NOTE - when using function, only 1 set of paranthesis will be found, instead use brackets for sets within functions!!  
  360. while((preg_match("/(". implode("|", $this->FNC) . ")\(([^\)\(]*(\([^\)]*\)[^\(\)]*)*[^\)\(]*)\)/", $infix, $match)) != 0) { 
  361. $func = $this->solveIF($match[2]); 
  362. switch($match[1]) { 
  363. case "cos": 
  364. $ans = cos($func); 
  365. break; 
  366. case "sin": 
  367. $ans = sin($func); 
  368. break; 
  369. case "tan": 
  370. $ans = tan($func); 
  371. break; 
  372. case "sec": 
  373. $tmp = cos($func); 
  374. if($tmp == 0) { 
  375. throw new Exception("Division by 0 on: 'sec({$func}) = 1/cos({$func})' in {$this->inFix}", EQEOS_E_DIV_ZERO); 
  376. return false; 
  377. $ans = 1/$tmp; 
  378. break; 
  379. case "csc": 
  380. $tmp = sin($func); 
  381. if($tmp == 0) { 
  382. throw new Exception("Division by 0 on: 'csc({$func}) = 1/sin({$func})' in {$this->inFix}", EQEOS_E_DIV_ZERO); 
  383. return false; 
  384. $ans = 1/$tmp; 
  385. break; 
  386. case "cot": 
  387. $tmp = tan($func); 
  388. if($tmp == 0) { 
  389. throw new Exception("Division by 0 on: 'cot({$func}) = 1/tan({$func})' in {$this->inFix}", EQEOS_E_DIV_ZERO); 
  390. return false; 
  391. $ans = 1/$tmp; 
  392. break; 
  393. default: 
  394. break; 
  395. $infix = str_replace($match[0], $ans, $infix); 
  396. if(DEBUG) 
  397. fclose($hand); 
  398. return $this->solvePF($this->in2post($infix)); 
  399.  
  400.  
  401. } //end function solveIF 
  402. } //end class 'eqEOS' 
  403.  
  404.  
  405. // fun class that requires the GD libraries to give visual output to the user  
  406. /** extends the eqEOS class so that it doesn't need to create it as a private var  
  407. - and it extends the functionality of that class */ 
  408. /** 
  409. * Equation Graph 
  410. * 
  411. * Fun class that requires the GD libraries to give visual output of an 
  412. * equation to the user. Extends the eqEOS class. 
  413. * 
  414. * @author Jon Lawrence <jlawrence11@gmail.com> 
  415. * @copyright Copyright *2005-2013 Jon Lawrence 
  416. * @license http://opensource.org/licenses/LGPL-2.1 LGPL 2.1 License 
  417. * @package Math 
  418. * @subpackage EOS 
  419. * @version 2.0 
  420. */ 
  421. class eqGraph extends eqEOS { 
  422. private $width; 
  423. private $height; 
  424. //GD Image reference 
  425. private $image; 
  426.  
  427. /** 
  428. * Constructor 
  429. * 
  430. * Sets up the Graph class with an image width and height defaults to 
  431. * 640x480 
  432. * 
  433. * @param Integer $width Image width 
  434. * @param Integer $height Image height 
  435. */ 
  436. public function __construct($width=640, $height=480) { 
  437. // default width and height equal to that of a poor monitor (in early 2000s) 
  438. $this->width = $width; 
  439. $this->height = $height; 
  440. //initialize main class variables 
  441. parent::__construct(); 
  442. } //end function eqGraph 
  443.  
  444.  
  445. /** 
  446. * Create GD Graph Image 
  447. * 
  448. * Creates a GD image based on the equation given with the parameters that are set 
  449. * 
  450. * @param String $eq Equation to use. Needs variable in equation to create graph, all variables are interpreted as 'x' 
  451. * @param Integer $xLow Lower x-bound for graph 
  452. * @param Integer $xHigh Upper x-bound for graph 
  453. * @param Float $xStep Stepping points while solving, the lower, the better precision. Slow if lower than .01 
  454. * @param Boolean $xyGrid Draw gridlines? 
  455. * @param Boolean $yGuess Guess the upper/lower yBounds? 
  456. * @param Integer $yLow Lower y-bound 
  457. * @param Integer $yHigh Upper y-bound 
  458. * @return Null 
  459. */ 
  460. public function graph($eq, $xLow, $xHigh, $xStep, $xyGrid = false, $yGuess = true, $yLow=false, $yHigh=false) { 
  461. //create our image and allocate the two colors 
  462. $img = ImageCreate($this->width, $this->height); 
  463. $white = ImageColorAllocate($img, 255, 255, 255); 
  464. $black = ImageColorAllocate($img, 0, 0, 0); 
  465. $grey = ImageColorAllocate($img, 220, 220, 220); 
  466. $xStep = abs($xStep); 
  467. //DEVELOPER, UNCOMMENT NEXT LINE IF WANTING TO PREVENT SLOW GRAPHS 
  468. //$xStep = ($xStep > .01) ? $xStep : 0.01; 
  469. if($xLow > $xHigh) 
  470. list($xLow, $xHigh) = array($xHigh, $xLow); //swap function 
  471.  
  472. $xScale = $this->width/($xHigh-$xLow); 
  473. $counter = 0; 
  474. if(DEBUG) { 
  475. $hand=fopen("eqgraph.txt", "w"); 
  476. fwrite($hand, "$eq\n"); 
  477. for($i=$xLow;$i<=$xHigh;$i+=$xStep) { 
  478. $tester = sprintf("%10.3f", $i); 
  479. if($tester == "-0.000") $i = 0; 
  480. $y = $this->solveIF($eq, $i); 
  481. //eval('$y='. str_replace('&x', $i, $eq).";"); /* used to debug my eqEOS class results */ 
  482. if(DEBUG) { 
  483. $tmp1 = sprintf("y(%5.3f) = %10.3f\n", $i, $y); 
  484. fwrite($hand, $tmp1); 
  485.  
  486. // If developer asked us to find the upper and lower bounds for y...  
  487. if($yGuess==true) { 
  488. $yLow = ($yLow===false || ($y<$yLow)) ? $y : $yLow; 
  489. $yHigh = ($yHigh===false || $y>$yHigh) ? $y : $yHigh; 
  490. $xVars[$counter] = $y; 
  491. $counter++;  
  492. if(DEBUG) 
  493. fclose($hand); 
  494. // add 0.01 to each side so that if y is from 1 to 5, the lines at 1 and 5 are seen  
  495. $yLow-=0.01;$yHigh+=0.01; 
  496.  
  497. //Now that we have all the variables stored...find the yScale 
  498. $yScale = $this->height/(($yHigh)-($yLow)); 
  499.  
  500. // if developer wanted a grid on the graph, add it now  
  501. if($xyGrid==true) { 
  502. for($i=ceil($yLow);$i<=floor($yHigh);$i++) { 
  503. $i0 = abs($yHigh-$i); 
  504. ImageLine($img, 0, $i0*$yScale, $this->width, $i0*$yScale, $grey); 
  505. for($i=ceil($xLow);$i<=floor($xHigh);$i++) { 
  506. $i0 = abs($xLow-$i); 
  507. ImageLine($img, $i0*$xScale, 0, $i0*$xScale, $this->height, $grey); 
  508.  
  509. //Now that we have the scales, let's see if we can draw an x/y-axis 
  510. if($xLow <= 0 && $xHigh >= 0) { 
  511. //the y-axis is within our range - draw it. 
  512. $x0 = abs($xLow)*$xScale; 
  513. ImageLine($img, $x0, 0, $x0, $this->height, $black); 
  514. for($i=ceil($yLow);$i<=floor($yHigh);$i++) { 
  515. $i0 = abs($yHigh-$i); 
  516. ImageLine($img, $x0-3, $i0*$yScale, $x0+3, $i0*$yScale, $black); 
  517. if($yLow <= 0 && $yHigh >= 0) { 
  518. //the x-axis is within our range - draw it. 
  519. $y0 = abs($yHigh)*$yScale; 
  520. ImageLine($img, 0, $y0, $this->width, $y0, $black); 
  521. for($i=ceil($xLow);$i<=floor($xHigh);$i++) { 
  522. $i0 = abs($xLow-$i); 
  523. ImageLine($img, $i0*$xScale, $y0-3, $i0*$xScale, $y0+3, $black); 
  524. $counter=2; 
  525.  
  526. //now graph it all ;] 
  527. for($i=$xLow+$xStep;$i<=$xHigh;$i+=$xStep) { 
  528. $x1 = (abs($xLow - ($i - $xStep)))*$xScale; 
  529. $y1 = (($xVars[$counter-1]<$yLow) || ($xVars[$counter-1] > $yHigh)) ? -1 : (abs($yHigh - $xVars[$counter-1]))*$yScale; 
  530. $x2 = (abs($xLow - $i))*$xScale; 
  531. $y2 = (($xVars[$counter]<$yLow) || ($xVars[$counter] > $yHigh)) ? -1 : (abs($yHigh - $xVars[$counter]))*$yScale; 
  532.  
  533. // if any of the y values were found to be off of the y-bounds, don't graph those connecting lines  
  534. if($y1!=-1 && $y2!=-1) 
  535. ImageLine($img, $x1, $y1, $x2, $y2, $black); 
  536. $counter++; 
  537. $this->image = $img; 
  538. } //end function 'graph' 
  539.  
  540. /** 
  541. * Sends JPG to browser 
  542. * 
  543. * Sends a JPG image with proper header to output 
  544. */ 
  545. public function outJPG() { 
  546. header("Content-type: image/jpeg"); 
  547. ImageJpeg($this->image); 
  548.  
  549. /** 
  550. * Sends PNG to browser 
  551. * 
  552. * Sends a PNG image with proper header to output 
  553. */ 
  554. function outPNG() { 
  555. header("Content-type: image/png"); 
  556. ImagePng($this->image); 
  557.  
  558. /** 
  559. * Output GD Image 
  560. * 
  561. * Will give the developer the GD resource for the graph that 
  562. * can be used to store the graph to the FS or other media 
  563. * 
  564. * @return Resource GD Image Resource 
  565. */ 
  566. public function getImage() { 
  567. return $this->image; 
  568.  
  569. /** 
  570. * Output GD Image 
  571. * 
  572. * Alias for eqGraph::getImage() 
  573. * 
  574. * @return Resource GD Image resource 
  575. */ 
  576. public function outGD() { 
  577. return $this->getImage(); 
  578. } //end class 'eqGraph' 
  579. ?> 
.