Skip to content

Novis\Image\Comparison::constructor

Makes a Comparison, the result of comparing two images, from its five values.

new Comparison takes identical, differingPixels, maxDelta, ssim and differenceImage, in that order. Each value becomes a read-only property of the same name. identical is true when no pixel differs. differingPixels is the number of pixels that differ, and maxDelta is the largest difference in one channel, from 0 to 255. ssim is a similarity score from 0 to 1, where 1 means the same image. differenceImage is a PNG with the differing pixels in red, or null.

Good to know: Image::compare makes a Comparison for you. You write new Comparison yourself only when you need one without comparing images, for example in a test of your own code.

Parameters

  • $identicalbooltrue when every pixel of the two images is the same
  • $differingPixelsuintthe number of pixels with a delta above tolerance
  • $maxDeltauintthe largest difference in one channel, from 0 to 255
  • $ssimfloatthe structural similarity of the two images, 1.0 for identical images
  • $differenceImage?bytesa PNG image with the differing pixels in red, or null. Image::compare draws it only with the option render: true.

Return value

Returns void.

Changelog

VersionChange
0.0.6Each 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

A comparison from its values

<?nvs
// Shows how to make a `Comparison` from its five values and read them back.
use Novis\Image\Comparison;
// 12 pixels differ, and no picture of the changes is stored, so `differenceImage` is `null`.
Comparison $result = new Comparison(false, 12, 40, 0.98, null);
echo 'identical: ', $result->identical ? 'yes' : 'no', "\n";
echo 'differing pixels: ', $result->differingPixels, "\n";
echo 'largest delta: ', $result->maxDelta, "\n";
echo 'similarity: ', $result->ssim, "\n";
echo 'has a picture: ', $result->differenceImage == null ? 'no' : 'yes', "\n";
Output
identical: no
differing pixels: 12
largest delta: 40
similarity: 0.98
has a picture: no

Test a report without images

<?nvs
// Shows how to check your own report code with a `Comparison` you make yourself, without any images.
use Novis\Image\Comparison;
class VisualReport
{
public static function line(string $page, Comparison $d): string
{
if ($d->identical) {
return $page . ': unchanged';
}
return $page . ': ' . $d->differingPixels . ' pixels changed';
}
}
// One result for each case the report handles.
echo VisualReport::line('home', new Comparison(true, 0, 0, 1.0, null)), "\n";
echo VisualReport::line('cart', new Comparison(false, 340, 255, 0.91, null)), "\n";
Output
home: unchanged
cart: 340 pixels changed

Compare with a saved result

<?nvs
// Shows how to check a new comparison against a result saved from an earlier run.
use Novis\Image\Color;
use Novis\Image\Comparison;
use Novis\Image\Image;
class Baseline
{
// A new result fails when more pixels differ than in the saved one.
public static function check(Comparison $saved, Comparison $now): string
{
if ($now->differingPixels > $saved->differingPixels) {
return 'worse: ' . $now->differingPixels . ' pixels differ, saved ' . $saved->differingPixels;
}
return 'ok: ' . $now->differingPixels . ' pixels differ';
}
}
// The earlier run had 64 differing pixels. Only the numbers were saved.
Comparison $saved = new Comparison(false, 64, 3, 0.99, null);
// Today one corner square of 16 by 16 pixels is a different colour.
Image $before = Image::create(32, 32, Color::hex('#2563eb'));
Image $after = $before->withOverlay(Image::create(16, 16, Color::hex('#2865ee')), { x: 0, y: 0 });
// This prints `worse: 256 pixels differ, saved 64`.
echo Baseline::check($saved, Image::compare($before, $after)), "\n";
Output
worse: 256 pixels differ, saved 64