Created
April 14, 2011 08:50
-
-
Save woodywang/919143 to your computer and use it in GitHub Desktop.
Render math formula with Google Chart API
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 | |
class GFormula | |
{ | |
private $_gate = 'https://chart.googleapis.com/chart'; | |
private $_params = array(); | |
private $_url; | |
public function __construct() | |
{ | |
$this->_params['cht'] = 'tx'; | |
} | |
public function setHeight($height) | |
{ | |
$this->_params['chs'] = $height; | |
} | |
public function setFormula($formula) | |
{ | |
$this->_params['chl'] = urlencode($formula); | |
} | |
public function setBackground($background) | |
{ | |
$this->_params['chf'] = $background; | |
} | |
public function setColor($color) | |
{ | |
$this->_params['chco'] = $color; | |
} | |
public function getUrl() | |
{ | |
$params = array(); | |
foreach($this->_params as $key => $value) | |
{ | |
$params[] = "$key=$value"; | |
} | |
$queryString = implode('&', $params); | |
$this->_url = $this->_gate . '?' . $queryString; | |
return $this->_url; | |
} | |
public function render() | |
{ | |
if(!$this->_url) | |
{ | |
$this->getUrl(); | |
} | |
$fp = fopen($this->_url, 'r'); | |
$img = file_get_contents($this->_url); | |
header('Content-Type: image/png'); | |
echo $img; | |
} | |
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment