Skip to content

Commit

Permalink
Using gd instead of imagemagick to help testing generated images
Browse files Browse the repository at this point in the history
  • Loading branch information
mazinsw committed Jul 23, 2018
1 parent 7363688 commit 06db8f9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
29 changes: 18 additions & 11 deletions tests/Thermal/Graphics/Filter/BayerOrderedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
29 changes: 18 additions & 11 deletions tests/Thermal/Graphics/Filter/FloydSteinbergTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions tests/Thermal/Graphics/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 06db8f9

Please sign in to comment.