diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-05-29 15:00:27 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-05-29 15:00:27 +0000 |
commit | a2be390c78a5b98655b08fc5c3da6c688011922d (patch) | |
tree | 5b92eea83e0389d35991cbe1cfadb8605885b25d /src/driver | |
parent | df6969d0a84ea8d4aa7ba0543ff48f556fad8363 (diff) | |
download | zetacomponents-graph-a2be390c78a5b98655b08fc5c3da6c688011922d.zip zetacomponents-graph-a2be390c78a5b98655b08fc5c3da6c688011922d.tar.gz |
- Partly implemented GD driver and updated checksums in tests
Diffstat (limited to 'src/driver')
-rw-r--r-- | src/driver/gd.php | 138 |
1 files changed, 133 insertions, 5 deletions
diff --git a/src/driver/gd.php b/src/driver/gd.php index d858aa0..5172334 100644 --- a/src/driver/gd.php +++ b/src/driver/gd.php @@ -14,11 +14,83 @@ */ class ezcGraphGdDriver extends ezcGraphDriver { + + /** + * Image ressource + * + * @var ressource + */ + protected $image; + public function __construct( array $options = array() ) { $this->options = new ezcGraphGdDriverOptions( $options ); } + protected function getImage() + { + if ( !isset( $this->image ) ) + { + $this->image = imagecreatetruecolor( $this->options->width, $this->options->height ); + imagecolorallocate( $this->image, 255, 255, 255 ); + } + + return $this->image; + } + + protected function allocate( ezcGraphColor $color ) + { + $image = $this->getImage(); + + if ( $color->alpha > 0 ) + { + $fetched = imagecolorexactalpha( $image, $color->red, $color->green, $color->blue, $color->alpha / 2 ); + if ( $fetched < 0 ) + { + $fetched = imagecolorallocatealpha( $image, $color->red, $color->green, $color->blue, $color->alpha / 2 ); + } + return $fetched; + } + else + { + $fetched = imagecolorexact( $image, $color->red, $color->green, $color->blue ); + if ( $fetched < 0 ) + { + $fetched = imagecolorallocate( $image, $color->red, $color->green, $color->blue ); + } + return $fetched; + } + } + + protected function imageCreateFrom( $file ) + { + $data = getimagesize( $file ); + + switch( $data[2] ) + { + case 1: + return array( + 'width' => $data[0], + 'height' => $data[1], + 'image' => imagecreatefromgif( $file ) + ); + case 2: + return array( + 'width' => $data[0], + 'height' => $data[1], + 'image' => imagecreatefromjpeg( $file ) + ); + case 3: + return array( + 'width' => $data[0], + 'height' => $data[1], + 'image' => imagecreatefrompng( $file ) + ); + default: + throw new ezcGraphGdDriverUnsupportedImageFormatException( $data[2] ); + } + } + /** * Draws a single polygon * @@ -29,7 +101,28 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawPolygon( array $points, ezcGraphColor $color, $filled = true ) { - + $image = $this->getImage(); + + $drawColor = $this->allocate( $color ); + + // Create point array + $pointCount = count( $points ); + $pointArray = array(); + for ( $i = 0; $i < $pointCount; ++$i ) + { + $pointArray[] = $points[$i]->x; + $pointArray[] = $points[$i]->y; + } + + // Draw polygon + if ( $filled ) + { + imagefilledpolygon( $image, $pointArray, $pointCount, $drawColor ); + } + else + { + imagepolygon( $image, $pointArray, $pointCount, $drawColor ); + } } /** @@ -42,7 +135,11 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawLine( ezcGraphCoordinate $start, ezcGraphCoordinate $end, ezcGraphColor $color ) { - + $image = $this->getImage(); + + $drawColor = $this->allocate( $color ); + + imageline( $image, $start->x, $start->y, $end->x, $end->y, $drawColor ); } /** @@ -105,7 +202,18 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawCircle( ezcGraphCoordinate $center, $width, $height, ezcGraphColor $color, $filled = true ) { - + $image = $this->getImage(); + + $drawColor = $this->allocate( $color ); + + if ( $filled ) + { + imagefilledellipse( $image, $center->x, $center->y, $width, $height, $drawColor ); + } + else + { + imageellipse( $image, $center->x, $center->y, $width, $height, $drawColor ); + } } /** @@ -119,7 +227,17 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawImage( $file, ezcGraphCoordinate $position, $width, $height ) { - + $imageFile = $this->imageCreateFrom( $file ); + $image = $this->getImage(); + + imagecopyresampled( + $image, + $imageFile['image'], + $position->x, $position->y, + 0, 0, + $position->x + $width, $position->y + $height, + $imageFile['width'], $imageFile['height'] + ); } /** @@ -130,7 +248,17 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function render ( $file ) { - + $image = $this->getImage(); + + switch ( $this->options->imageFormat ) + { + case IMG_PNG: + imagepng( $image, $file ); + break; + case IMG_JPEG: + imagejpeg( $image, $file, $this->options->quality ); + break; + } } } |