Skip to content

Instantly share code, notes, and snippets.

@bazilio91
Created October 18, 2012 06:00
Show Gist options
  • Save bazilio91/3910116 to your computer and use it in GitHub Desktop.
Save bazilio91/3910116 to your computer and use it in GitHub Desktop.
PhpUnit image assert function
protected function assertImage($expected, $actual, $message = '')
{
$descriptors = array(
array('pipe', 'r'),
array('pipe', 'w'),
array('pipe', 'w'),
);
$command = 'compare -metric RMSE ' . escapeshellarg($expected) . ' ' . escapeshellarg($actual) . ' /dev/null';
$proc = proc_open($command, $descriptors, $pipes);
$diff = stream_get_contents($pipes[2]);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
preg_match('#\((.+)\)#', $diff, $match);
$threshold = floatval($match[1]);
$this->assertLessThan(0.05, $threshold, $message);
}
@agopaul
Copy link

agopaul commented Apr 17, 2015

Requires imagemagick installed (not PHP Imagick extension)

sudo apt-get install imagemagick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment