* @package Mammut\Chart */ class ImageRenderer extends \Mammut\StdObject implements iRenderer { private $imgType; private $img; /** * Creates a new ImageRenderer object * * @param string $imgType * one of the IMG_* constants of the GDLib extension, default is IMG_PNG */ public function __construct($imgType = IMG_PNG) { $this->imgType = $imgType; } protected function selectColor(Color $c) { return imagecolorallocate($this->img, $c->getRed(), $c->getGreen(), $c->getBlue()); } protected function freeColor($id) { imagecolordeallocate($this->img, $id); } public function start($w, $h, Color $bg, $hint) { $this->img = imagecreatetruecolor($w, $h); if (!is_null($bg)) { $c = $this->selectColor($bg); imagefilledrectangle($this->img, 0, 0, $w, $h, $c); $this->freeColor($c); } imageantialias($this->img, true); } public function drawLine($x1 , $y1 , $x2 , $y2, Color $color) { $c = $this->selectColor($color); imageline($this->img, $x1, $y1, $x2, $y2, $c); $this->freeColor($c); } public function drawRect($x1 , $y1 , $x2 , $y2, Color $color, $fill = FALSE) { $c = $this->selectColor($color); if ($fill) imagefilledrectangle($this->img, $x1 , $y1 , $x2 , $y2, $c); else imagerectangle($this->img, $x1 , $y1 , $x2 , $y2, $c); $this->freeColor($c); } public function drawText($x, $y, $text, Color $color, $font = NULL, $size = -1, $angle = 0) { $c = $this->selectColor($color); imagestring($this->img, 1, $x, $y, $text, $c); $this->freeColor($c); } public function drawEllipse($cx , $cy , $width , $height , Color $color, $fill = FALSE) { $c = $this->selectColor($color); if ($fill) imagefilledellipse($this->img, $cx, $cy, $width, $height, $c); else imageellipse($this->img, $cx, $cy, $width, $height, $c); $this->freeColor($c); } public function drawArc($cx , $cy , $width , $height, $start, $end , Color $color, $fill = FALSE) { $c = $this->selectColor($color); if ($fill) imagefilledarc($this->img, $cx, $cy, $width, $height , $start , $end , $c, IMG_ARC_PIE); else imagearc($this->img, $cx, $cy, $width, $height, $start , $end , $c, IMG_ARC_PIE); $this->freeColor($c); } public function output($file = NULL) { if(is_null($file)) imagepng($this->img); else { if ($file instanceof iInput) { // TODO: find a better way to do this ob_start(); imagepng($this->img); $file->write(ob_get_contents()); ob_end_clean(); } else imagepng($this->img, $file); } } public function __destruct() { imagedestroy($this->img); $this->img = NULL; } }