Storing a pair of source.png and expected_result.png (once generated by the software, verified as good and saved as a reference image) will suffice. The implementation of the comparison function seems overhead.
The main purpose of unit tests is to signal whether the behavior of the system is changing, and what this test will do if the newly created sketch does not match the reference.
Nevertheless, if for some reason the software generates slightly different images each time, then if this is not an error, use the proposed approach for comparing similar images.
What to do if image content is different
In the case of PNG files used in this example, their contents may contain some supporting information, such as EXIF.
Therefore, you may have to try to create a copy of the image without this additional information. Check if the following code works for you:
public function testThumbnails() { $this->assertPngImageContentsEquals(__DIR__ . '/test1.png', __DIR__ . '/test2.png'); } public static function assertPngImageContentsEquals( $expected, $actual, $message = 'Contents of PNG files differ' ) { self::assertFileExists($expected, $message); self::assertFileExists($actual, $message); $copy_expected = self::_makePngCopy($expected, __DIR__ . '/expected.png'); $copy_actual = self::_makePngCopy($actual, __DIR__ . '/actual.png'); var_dump($copy_expected); var_dump($copy_actual); self::assertFileEquals($copy_expected, $copy_actual, 'Thumbnails differ'); unlink($copy_expected); unlink($copy_actual); } private static function _makePngCopy($sourceFile, $resultFile) { $image = imagecreatefrompng($sourceFile); imagepng($image, $resultFile); imagedestroy($image); return $resultFile; }
source share