summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKore Nordmann <github@kore-nordmann.de>2006-08-09 09:48:46 +0000
committerKore Nordmann <github@kore-nordmann.de>2006-08-09 09:48:46 +0000
commit3c5cb5cd65c6954f78ba0bd1ccbba44cb2ae4a9a (patch)
treec767f3a3008d4b67e7b2f1d1db84ead32278de8c
parentdd6a8766a0c6749c675e9f1ebe4db38a7c9df198 (diff)
downloadzetacomponents-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
-rw-r--r--src/driver/gd.php109
-rw-r--r--src/options/gd_driver.php37
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.pngbin4117 -> 4112 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.pngbin4180 -> 4170 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.pngbin4179 -> 4169 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.pngbin4126 -> 4119 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.pngbin4126 -> 4119 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.pngbin675 -> 674 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.pngbin0 -> 21868 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.pngbin13619 -> 14851 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.pngbin0 -> 31225 bytes
-rw-r--r--tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.pngbin0 -> 28621 bytes
-rw-r--r--tests/driver_gd_test.php87
13 files changed, 199 insertions, 34 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;
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png
index 407b1dc..58ac21a 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png
index 7b74a24..597c6ca 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png
index aa97a41..9946a1e 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png
index 7b3a73a..96ba490 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png
index ecc86bd..8bb1c2c 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png
index 5555bb8..fef523f 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png
new file mode 100644
index 0000000..d688097
--- /dev/null
+++ b/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png
index 2df6ad2..e692c83 100644
--- a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png
+++ b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png
new file mode 100644
index 0000000..4b2bb78
--- /dev/null
+++ b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png
Binary files differ
diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png
new file mode 100644
index 0000000..bc88cea
--- /dev/null
+++ b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png
Binary files differ
diff --git a/tests/driver_gd_test.php b/tests/driver_gd_test.php
index 6390d83..8624d03 100644
--- a/tests/driver_gd_test.php
+++ b/tests/driver_gd_test.php
@@ -127,7 +127,7 @@ class ezcGraphGdDriverTest extends ezcImageTestCase
new ezcGraphCoordinate( 122, 34 ),
new ezcGraphCoordinate( 12, 71 ),
),
- ezcGraphColor::fromHex( '#3465A4BB' ),
+ ezcGraphColor::fromHex( '#3465A47F' ),
true
);
@@ -1125,6 +1125,91 @@ class ezcGraphGdDriverTest extends ezcImageTestCase
);
}
+ public function testSupersamplingDrawImagePngWithBackground()
+ {
+ $filename = $this->tempDir . __FUNCTION__ . '.png';
+ $this->driver->options->supersampling = 2;
+ $this->driver->options->background = $this->basePath . $this->testFiles['png'];
+
+ $this->driver->drawImage(
+ $this->basePath . $this->testFiles['jpeg'],
+ new ezcGraphCoordinate( 10, 10 ),
+ 100,
+ 50
+ );
+
+ $this->driver->render( $filename );
+
+ $this->assertTrue(
+ file_exists( $filename ),
+ 'No image was generated.'
+ );
+
+ $this->assertImageSimilar(
+ $filename,
+ $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.png',
+ 'Image does not look as expected.',
+ 10
+ );
+ }
+
+ public function testSupersamplingDrawTransparentCircleFilledWithBackground()
+ {
+ $filename = $this->tempDir . __FUNCTION__ . '.png';
+ $this->driver->options->supersampling = 2;
+ $this->driver->options->background = $this->basePath . $this->testFiles['png'];
+
+ $this->driver->drawCircle(
+ new ezcGraphCoordinate( 100, 50 ),
+ 80,
+ 40,
+ ezcGraphColor::fromHex( '#3465A47F' )
+ );
+
+ $this->driver->render( $filename );
+
+ $this->assertTrue(
+ file_exists( $filename ),
+ 'No image was generated.'
+ );
+
+ $this->assertImageSimilar(
+ $filename,
+ $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.png',
+ 'Image does not look as expected.',
+ 10
+ );
+ }
+
+ public function testRisizedSupersamplingDrawTransparentCircleFilledWithBackground()
+ {
+ $filename = $this->tempDir . __FUNCTION__ . '.png';
+ $this->driver->options->supersampling = 2;
+ $this->driver->options->background = $this->basePath . $this->testFiles['png'];
+ $this->driver->options->resampleFunction = 'imagecopyresized';
+
+ $this->driver->drawCircle(
+ new ezcGraphCoordinate( 100, 50 ),
+ 80,
+ 40,
+ ezcGraphColor::fromHex( '#3465A47F' )
+ );
+
+ $this->driver->render( $filename );
+
+ $this->assertTrue(
+ file_exists( $filename ),
+ 'No image was generated.'
+ );
+
+ $this->assertImageSimilar(
+ $filename,
+ $this->basePath . 'compare/' . __CLASS__ . '_' . __FUNCTION__ . '.png',
+ 'Image does not look as expected.',
+ 10
+ );
+ }
+
public function testSupersamplingDrawTextBoxShortString()
{
$filename = $this->tempDir . __FUNCTION__ . '.png';
OpenPOWER on IntegriCloud