diff options
author | Kore Nordmann <github@kore-nordmann.de> | 2006-08-09 09:48:46 +0000 |
---|---|---|
committer | Kore Nordmann <github@kore-nordmann.de> | 2006-08-09 09:48:46 +0000 |
commit | 3c5cb5cd65c6954f78ba0bd1ccbba44cb2ae4a9a (patch) | |
tree | c767f3a3008d4b67e7b2f1d1db84ead32278de8c /src | |
parent | dd6a8766a0c6749c675e9f1ebe4db38a7c9df198 (diff) | |
download | zetacomponents-graph-3c5cb5cd65c6954f78ba0bd1ccbba44cb2ae4a9a.zip zetacomponents-graph-3c5cb5cd65c6954f78ba0bd1ccbba44cb2ae4a9a.tar.gz |
- Added possibility to set a background for gd driver
- Postprocess images in GD driver t prevent from additional resampling of
images
- Made used resampling function customizeable
Diffstat (limited to 'src')
-rw-r--r-- | src/driver/gd.php | 109 | ||||
-rw-r--r-- | src/options/gd_driver.php | 37 |
2 files changed, 113 insertions, 33 deletions
diff --git a/src/driver/gd.php b/src/driver/gd.php index 2de0af9..cc87824 100644 --- a/src/driver/gd.php +++ b/src/driver/gd.php @@ -23,6 +23,13 @@ class ezcGraphGdDriver extends ezcGraphDriver protected $image; /** + * Array with image files to draw + * + * @var array + */ + protected $preProcessImages = array(); + + /** * List of strings to draw * array ( array( * 'text' => array( 'strings' ), @@ -594,23 +601,42 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function drawImage( $file, ezcGraphCoordinate $position, $width, $height ) { - $imageFile = $this->imageCreateFrom( $file ); - $image = $this->getImage(); - - imagecopyresampled( - $image, - $imageFile['image'], - $this->supersample( $position->x ), - $this->supersample( $position->y ), - 0, - 0, - $this->supersample( $width ), - $this->supersample( $height ), - $imageFile['width'], $imageFile['height'] + $this->preProcessImages[] = array( + 'file' => $file, + 'position' => $position, + 'width' => $width, + 'height' => $height, ); } /** + * Draw all images to image ressource handler + * + * @param ressource $image Image to draw on + * @return ressource Updated image ressource + */ + protected function addImages( $image ) + { + foreach ( $this->preProcessImages as $preImage ) + { + $preImageData = $this->imageCreateFrom( $preImage['file'] ); + call_user_func_array( + $this->options->resampleFunction, + array( + $image, + $preImageData['image'], + $preImage['position']->x, $preImage['position']->y, + 0, 0, + $preImage['width'], $preImage['height'], + $preImageData['width'], $preImageData['height'], + ) + ); + } + + return $image; + } + + /** * Finally save image * * @param mixed $file @@ -618,32 +644,51 @@ class ezcGraphGdDriver extends ezcGraphDriver */ public function render ( $file ) { - if ( ( $supersampling = $this->options->supersampling ) > 1 ) + $destination = imagecreatetruecolor( $this->options->width, $this->options->height ); + + // Default to a transparent white background + $bgColor = imagecolorallocatealpha( $destination, 255, 255, 255, 127 ); + imagealphablending( $destination, true ); + imagesavealpha( $destination, true ); + imagefill( $destination, 1, 1, $bgColor ); + + // Apply background if one is defined + if ( $this->options->background !== false ) { - // Supersampling active, resample image - $image = $this->getImage(); - $sampled = imagecreatetruecolor( $this->options->width, $this->options->height ); + $background = $this->imageCreateFrom( $this->options->background ); + + call_user_func_array( + $this->options->resampleFunction, + array( + $destination, + $background['image'], + 0, 0, + 0, 0, + $this->options->width, $this->options->height, + $background['width'], $background['height'], + ) + ); + } - // Default to a transparent white background - $bgColor = imagecolorallocatealpha( $sampled, 255, 255, 255, 127 ); - imagealphablending( $sampled, true ); - imagesavealpha( $sampled, true ); - imagefill( $sampled, 1, 1, $bgColor ); + // Draw all images to exclude them from supersampling + $destination = $this->addImages( $destination ); - imagecopyresampled( - $sampled, + // Finally merge with graph + $image = $this->getImage(); + call_user_func_array( + $this->options->resampleFunction, + array( + $destination, $image, 0, 0, 0, 0, - $this->options->width, - $this->options->height, - $this->supersample( $this->options->width ), - $this->supersample( $this->options->height ) - ); + $this->options->width, $this->options->height, + $this->supersample( $this->options->width ), $this->supersample( $this->options->height ) + ) + ); - $this->image = $sampled; - imagedestroy( $image ); - } + $this->image = $destination; + imagedestroy( $image ); // Draw all texts $this->drawAllTexts(); diff --git a/src/options/gd_driver.php b/src/options/gd_driver.php index ef8b3b5..39ceece 100644 --- a/src/options/gd_driver.php +++ b/src/options/gd_driver.php @@ -47,6 +47,20 @@ class ezcGraphGdDriverOptions extends ezcGraphDriverOptions protected $supersampling = 2; /** + * Background image to put the graph on + * + * @var string + */ + protected $background = false; + + /** + * Function used to resample / resize images + * + * @var string + */ + protected $resampleFunction = 'imagecopyresampled'; + + /** * Set an option value * * @param string $propertyName @@ -66,7 +80,7 @@ class ezcGraphGdDriverOptions extends ezcGraphDriverOptions } else { - throw new ezcBaseValueException( $propertyValue, 'Unsupported image type.' ); + throw new ezcBaseValueException( $propertyName, $propertyValue, 'Unsupported image type.' ); } break; case 'detail': @@ -78,6 +92,27 @@ class ezcGraphGdDriverOptions extends ezcGraphDriverOptions case 'supersampling': $this->supersampling = (int) max( 1, $propertyValue ); break; + case 'background': + if ( $propertyValue === false || + ( is_file( $propertyValue ) && is_readable( $propertyValue ) ) ) + { + $this->background = realpath( $propertyValue ); + } + else + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'readable file' ); + } + break; + case 'resampleFunction': + if ( function_exists( $propertyValue ) ) + { + $this->resampleFunction = $propertyValue; + } + else + { + throw new ezcBaseValueException( $propertyName, $propertyValue, 'function' ); + } + break; default: parent::__set( $propertyName, $propertyValue ); break; |