From 06db8f994411a41efd6f45eea94e0ff1aafcad33 Mon Sep 17 00:00:00 2001 From: Francimar Alves Date: Mon, 23 Jul 2018 17:48:02 -0300 Subject: [PATCH] Using gd instead of imagemagick to help testing generated images --- composer.json | 4 +-- .../Graphics/Filter/BayerOrderedTest.php | 29 ++++++++++++------- .../Graphics/Filter/FloydSteinbergTest.php | 29 ++++++++++++------- tests/Thermal/Graphics/ImageTest.php | 14 +++++++++ 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index cd3dd19..e09ed45 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,9 @@ "ext-mbstring": "*" }, "require-dev": { - "ext-imagick": "*", "phpunit/phpunit": "5.*", "scrutinizer/ocular": "@stable", - "squizlabs/php_codesniffer": "@stable", - "lupka/phpunit-compare-images": "^1.0" + "squizlabs/php_codesniffer": "@stable" }, "autoload": { "psr-4": { diff --git a/tests/Thermal/Graphics/Filter/BayerOrderedTest.php b/tests/Thermal/Graphics/Filter/BayerOrderedTest.php index bd54dd6..9f19283 100644 --- a/tests/Thermal/Graphics/Filter/BayerOrderedTest.php +++ b/tests/Thermal/Graphics/Filter/BayerOrderedTest.php @@ -3,29 +3,36 @@ namespace Thermal\Graphics\Filter; use Thermal\PrinterTest; -use Lupka\PHPUnitCompareImages\CompareImagesTrait; +use Thermal\Graphics\ImageTest; class BayerOrderedTest extends \PHPUnit_Framework_TestCase { - use CompareImagesTrait; - public function testProcess() { $image_path = dirname(dirname(dirname(__DIR__))) . '/resources/sample.jpg'; $image = imagecreatefromjpeg($image_path); $filter = new BayerOrdered(); - $new_image = $filter->process($image); + $result_image = $filter->process($image); imagedestroy($image); ob_start(); - imagepng($new_image); + imagepng($result_image); $image_data = ob_get_contents(); ob_end_clean(); - imagedestroy($new_image); - $result_image = new \Imagick(); - $result_image->readImageBlob($image_data); $expected_data = PrinterTest::getExpectedBuffer('sample_bayer_ordered.png', $image_data); - $expected_image = new \Imagick(); - $expected_image->readImageBlob($expected_data); - $this->assertImagesSame($expected_image, $result_image); + $expected_image = imagecreatefromstring($expected_data); + $this->assertEquals( + [ + 'width' => imagesx($expected_image), + 'height' => imagesy($expected_image), + 'pixels' => ImageTest::extractColors($expected_image) + ], + [ + 'width' => imagesx($result_image), + 'height' => imagesy($result_image), + 'pixels' => ImageTest::extractColors($result_image) + ] + ); + imagedestroy($expected_image); + imagedestroy($result_image); } } diff --git a/tests/Thermal/Graphics/Filter/FloydSteinbergTest.php b/tests/Thermal/Graphics/Filter/FloydSteinbergTest.php index c623625..cefb7ba 100644 --- a/tests/Thermal/Graphics/Filter/FloydSteinbergTest.php +++ b/tests/Thermal/Graphics/Filter/FloydSteinbergTest.php @@ -3,29 +3,36 @@ namespace Thermal\Graphics\Filter; use Thermal\PrinterTest; -use Lupka\PHPUnitCompareImages\CompareImagesTrait; +use Thermal\Graphics\ImageTest; class FloydSteinbergTest extends \PHPUnit_Framework_TestCase { - use CompareImagesTrait; - public function testProcess() { $image_path = dirname(dirname(dirname(__DIR__))) . '/resources/sample.jpg'; $image = imagecreatefromjpeg($image_path); $filter = new FloydSteinberg(); - $new_image = $filter->process($image); + $result_image = $filter->process($image); imagedestroy($image); ob_start(); - imagepng($new_image); + imagepng($result_image); $image_data = ob_get_contents(); ob_end_clean(); - imagedestroy($new_image); - $result_image = new \Imagick(); - $result_image->readImageBlob($image_data); $expected_data = PrinterTest::getExpectedBuffer('sample_floyd_steinberg.png', $image_data); - $expected_image = new \Imagick(); - $expected_image->readImageBlob($expected_data); - $this->assertImagesSame($expected_image, $result_image); + $expected_image = imagecreatefromstring($expected_data); + $this->assertEquals( + [ + 'width' => imagesx($expected_image), + 'height' => imagesy($expected_image), + 'pixels' => ImageTest::extractColors($expected_image) + ], + [ + 'width' => imagesx($result_image), + 'height' => imagesy($result_image), + 'pixels' => ImageTest::extractColors($result_image) + ] + ); + imagedestroy($expected_image); + imagedestroy($result_image); } } diff --git a/tests/Thermal/Graphics/ImageTest.php b/tests/Thermal/Graphics/ImageTest.php index e8dba3f..4dd600a 100644 --- a/tests/Thermal/Graphics/ImageTest.php +++ b/tests/Thermal/Graphics/ImageTest.php @@ -6,6 +6,20 @@ class ImageTest extends \PHPUnit_Framework_TestCase { + public static function extractColors($image) + { + $colors = []; + $width = imagesx($image); + $height = imagesy($image); + for ($y = 0; $y < $height; $y++) { + for ($x = 0; $x < $width; $x++) { + $color = imagecolorsforindex($image, imagecolorat($image, $x, $y)); + $colors[] = $color; + } + } + return $colors; + } + public function testCreateFromData() { $image_path = dirname(dirname(__DIR__)) . '/resources/sample.jpg';