Skip to content

Instantly share code, notes, and snippets.

@woodywang
Created April 14, 2011 08:50
Show Gist options
  • Save woodywang/919143 to your computer and use it in GitHub Desktop.
Save woodywang/919143 to your computer and use it in GitHub Desktop.
Render math formula with Google Chart API
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment