Novis\Image\Image::compare
Novis\Image\Image::compare(
Core\Bytes::Source|Image $a,
Core\Bytes::Source|Image $b,
?{ tolerance?: uint, render?: bool } $options = null,
): ComparisonCompares two images of the same size and returns a Comparison with the result.
Image::compare takes two images. Each one is an Image value, bytes, an open Core\IO\File or an uploaded Core\Request\Part. The Comparison it returns has five values. identical is true when every pixel is the same. differingPixels is the number of pixels that are not the same. maxDelta is the largest difference in one colour channel, from 0 to 255. ssim is a score from 0.0 to 1.0 for how similar the two images look, and it is 1.0 for identical images.
A pixel counts as different only when its difference is above tolerance, which is 0 by default. With render: true, differenceImage is a PNG file that shows the differing pixels in red. Otherwise differenceImage is null. Two images of different sizes throw a LogicError.
Good to know: in a test, check a number of the Comparison. For example, test that ssim is at least 0.99, so a small change from a new image encoder does not fail the test.
The examples below show a test that checks ssim, a tolerance for small changes, and the error for two sizes.
Parameters
$aCore\Bytes::Source|Image$bCore\Bytes::Source|Image$options?{ tolerance?: uint, render?: bool }default null
Return value
Returns Comparison.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Image\Image method that returns a new image is named for it, for example resized(), blurred() and withText(). keepingMetadata() replaces metadata({ keep: true }), and encodeVariants() replaces variants(). Image::compare returns a Comparison with the property differenceImage, which was Diff with diff. |
Examples
Check a thumbnail
<?nvs// Shows how a test checks that a JPEG thumbnail still looks like the original PNG.use Novis\Image\Format;use Novis\Image\Image;
// A pattern of 4 by 4 grey pixels, made 64 by 64 pixels wide.Image $photo = Image::fromRaw({ width: 4, height: 4, pixels: Core\Encoding::fromHex( '202020ffe0e0e0ff606060ffa0a0a0ff' . '909090ff303030ffd0d0d0ff505050ff' . 'f0f0f0ff707070ff101010ffc0c0c0ff' . '404040ffb0b0b0ff808080ff000000ff', ),})->resized({ width: 64, height: 64 });
// JPEG changes some pixels a little, so the images are not identical.tainted bytes $jpeg = $photo->withFormat(Format::Jpeg, { quality: 90 })->encode();var $diff = Image::compare($photo, $jpeg);
echo 'identical: ', $diff->identical ? 'yes' : 'no', "\n";echo 'similarity: ', Core\Math::round($diff->ssim, { precision: 3 }), "\n";echo 'looks the same: ', $diff->ssim >= 0.95 ? 'yes' : 'no', "\n";identical: nosimilarity: 0.999looks the same: yesAllow small changes
<?nvs// Shows how `tolerance` ignores small changes, and how `render` returns a picture of the changes.use Novis\Image\Color;use Novis\Image\Image;
// The second image has one corner square in a slightly different blue.Image $before = Image::create(32, 32, Color::hex('#2563eb'));Image $after = $before->withOverlay(Image::create(8, 8, Color::hex('#2865ee')), { x: 0, y: 0 });
// With no tolerance, the 64 pixels of the square count as different.var $exact = Image::compare($before, $after, { render: true });echo $exact->differingPixels, ' pixels differ, largest delta ', $exact->maxDelta, "\n";
// `differenceImage` is a PNG file that shows those pixels in red.echo 'diff image: ', Image::info($exact->differenceImage ?? '' as bytes)->width, " pixels wide\n";
// A tolerance of 5 is larger than every delta, so no pixel counts as different.echo Image::compare($before, $after, { tolerance: 5 })->differingPixels, " pixels differ\n";64 pixels differ, largest delta 3diff image: 32 pixels wide0 pixels differSizes must match
<?nvs// Shows the error `Image::compare` throws for two images of different sizes.use Novis\Image\Color;use Novis\Image\Image;
Image $small = Image::create(32, 32, Color::hex('#2563eb'));Image $large = Image::create(64, 64, Color::hex('#2563eb'));
// The sizes are different, so this throws a `LogicError`.try { echo Image::compare($small, $large)->ssim, "\n";} catch (LogicError $e) { echo 'LogicError: ', $e->message, "\n";}LogicError: the images are 32x32 and 64x64, and `compare` needs two images of the same size