Skip to content

Instantly share code, notes, and snippets.

@foo9
Created May 5, 2015 10:09
Show Gist options
  • Save foo9/6d3d62ff96288738ffc8 to your computer and use it in GitHub Desktop.
Save foo9/6d3d62ff96288738ffc8 to your computer and use it in GitHub Desktop.
<?php
namespace Foo9\Img;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
class Compare
{
/** @var integer */
private $fuzz;
/** @var integer */
private $timeout;
/**
*
*
* @param integer $fuzz
* @param integer $timeout
*/
public function __construct($fuzz, $timeout = 3600)
{
$this->fuzz = $fuzz;
$this->timeout = $timeout;
}
/**
*
*
* @param string $base
* @param string $compare
* @param string $output
*
* @throws RuntimeException
*
* @return integer
*/
public function run($base, $compare, $output)
{
$cmdline = self::createCmdline($this->fuzz, $base, $compare, $output);
$process = new Process($cmdline);
$process->setTimeout($this->timeout);
$process->run();
// @see http://www.imagemagick.org/script/compare.php
// The compare program returns 2 on error otherwise 0
// if the images are similar or 1 if they are dissimilar.
$exitCode = $process->getExitCode();
switch ($exitCode) {
case 0:
case 1:
$diffPixels = $process->getErrorOutput();
break;
case 2:
default:
throw new \RuntimeException($process->getErrorOutput());
}
$size = getimagesize($output);
$allPixels = $size[0] * $size[1];
return round(($diffPixels / $allPixels) * 100);
}
/**
*
*
* @param integer $fuzz
* @param string $base
* @param string $compare
* @param string $output
*
* @return string
*/
public static function createCmdline($fuzz, $base, $compare, $output)
{
$arguments = ProcessBuilder::create(array(
$base,
$compare,
$output
))
->getProcess()
->getCommandLine();
return implode(' ', array(
'/usr/local/bin/compare',
'-dissimilarity-threshold 1',
'-fuzz ' . (int) $fuzz . '%',
'-metric AE',
'-highlight-color blue',
$arguments
));
}
}
<?php
namespace Foo9\Img;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
class Crop
{
/** @var integer */
private $timeout;
/**
*
*
* @param integer $timeout
*/
public function __construct($timeout = 3600)
{
$this->timeout = $timeout;
}
/**
*
*
* @param string $base
* @param string $compare
* @param string $output
*
* @throws RuntimeException
*
* @return void
*/
public function run($base, $compare)
{
$cmdline = self::createCmdline($base, $compare);
$process = new Process($cmdline);
$process->setTimeout($this->timeout);
$process->run();
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
}
/**
*
*
* @param string $base
* @param string $compare
*
* @return string
*/
public static function createCmdline($base, $compare)
{
list($width, $baseHeight) = getimagesize($base);
$compareHeight = getimagesize($compare)[1];
if ($baseHeight > $compareHeight) {
$imageToCrop = $compare;
$heightToCropTo = $baseHeight;
} else {
$imageToCrop = $base;
$heightToCropTo = $compareHeight;
}
$crop = ProcessBuilder::create(array(
$imageToCrop
))
->getProcess()
->getCommandLine();
return implode(' ', array(
'/usr/local/bin/convert',
"${crop}",
'-background none',
"-extent ${width}x${heightToCropTo}",
"${crop}"
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment