summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/driver/gd.php109
-rw-r--r--src/options/gd_driver.php37
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;
OpenPOWER on IntegriCloud