From 3c5cb5cd65c6954f78ba0bd1ccbba44cb2ae4a9a Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Wed, 9 Aug 2006 09:48:46 +0000 Subject: - 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 --- src/driver/gd.php | 109 +++++++++++++++------ src/options/gd_driver.php | 37 ++++++- ...dDriverTest_testDrawTextBoxLongSpacedString.png | Bin 4117 -> 4112 bytes ...rTest_testDrawTextBoxLongSpacedStringCenter.png | Bin 4180 -> 4170 bytes ...testDrawTextBoxLongSpacedStringCenterBottom.png | Bin 4179 -> 4169 bytes ...erTest_testDrawTextBoxLongSpacedStringRight.png | Bin 4126 -> 4119 bytes ..._testDrawTextBoxLongSpacedStringRightMiddle.png | Bin 4126 -> 4119 bytes ...raphGdDriverTest_testDrawTransparentPolygon.png | Bin 675 -> 674 bytes ...ngDrawTransparentCircleFilledWithBackground.png | Bin 0 -> 21868 bytes ...hGdDriverTest_testSupersamplingDrawImagePng.png | Bin 13619 -> 14851 bytes ...testSupersamplingDrawImagePngWithBackground.png | Bin 0 -> 31225 bytes ...ngDrawTransparentCircleFilledWithBackground.png | Bin 0 -> 28621 bytes tests/driver_gd_test.php | 87 +++++++++++++++- 13 files changed, 199 insertions(+), 34 deletions(-) create mode 100644 tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png create mode 100644 tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png create mode 100644 tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png 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 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedString.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png index 7b74a24..597c6ca 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenter.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png index aa97a41..9946a1e 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringCenterBottom.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png index 7b3a73a..96ba490 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRight.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png index ecc86bd..8bb1c2c 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTextBoxLongSpacedStringRightMiddle.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png b/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png index 5555bb8..fef523f 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png and b/tests/data/compare/ezcGraphGdDriverTest_testDrawTransparentPolygon.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png new file mode 100644 index 0000000..d688097 Binary files /dev/null and b/tests/data/compare/ezcGraphGdDriverTest_testRisizedSupersamplingDrawTransparentCircleFilledWithBackground.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png index 2df6ad2..e692c83 100644 Binary files a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png and b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePng.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png new file mode 100644 index 0000000..4b2bb78 Binary files /dev/null and b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawImagePngWithBackground.png differ diff --git a/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png new file mode 100644 index 0000000..bc88cea Binary files /dev/null and b/tests/data/compare/ezcGraphGdDriverTest_testSupersamplingDrawTransparentCircleFilledWithBackground.png 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'; -- cgit v1.1