Skip to content

Instantly share code, notes, and snippets.

@meglio
Created July 6, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save meglio/3059431 to your computer and use it in GitHub Desktop.
Save meglio/3059431 to your computer and use it in GitHub Desktop.
static function detect($path1, $path2, $colorPrecision = null, $gaussianBlur = false)
{
$img1 = imagecreatefrompng($path1);
$img2 = imagecreatefrompng($path2);
$w1 = imagesx($img1); $h1 = imagesy($img1);
$w2 = imagesx($img2); $h2 = imagesy($img2);
# Stop early if the size of images such that images can not be part of each other
if (($w1 > $w2 && $h2 > $h1))
return 0;
if ($w2 > $w1 && $h1 > $h2)
return 0;
if ($gaussianBlur)
{
imagefilter($img1, IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($img2, IMG_FILTER_GAUSSIAN_BLUR);
}
# Find which image can be part of which image
if ($w1 > $w2 || $h1 > $h2
|| ($w1 == $w2 && $h1 >= $h2)
|| ($h1 == $h2 && $w1 >= $w2))
{
$full = $img1; $wf = $w1; $hf = $h1;
$part = $img2; $wp = $w2; $hp = $h2;
$potentialResult = 1;
}
else
{
$full = $img2; $wf = $w2; $hf = $h2;
$part = $img1; $wp = $w1; $hp = $h1;
$potentialResult = -1;
}
$rgbFull = $rgbPart = array();
$maxX = $wf-$wp;
$maxY = $hf-$hp;
# $left, $top - coordinate on the full image where we place the part image
for($left = 0; $left <= $maxX; $left++)
for($top = 0; $top <= $maxY; $top++)
{
# Loop over part image
$imageFailed = false;
for ($xi = 0; $xi < $wp; $xi++)
{
for ($yi = 0; $yi < $hp; $yi++)
{
if (!self::sameColors($full, $left+$xi, $top+$yi, $part, $xi, $yi, $colorPrecision))
{
$imageFailed = true;
break;
}
}
if ($imageFailed)
break;
}
if (!$imageFailed)
{
imagedestroy($img1);
imagedestroy($img2);
return $potentialResult;
}
}
imagedestroy($img1);
imagedestroy($img2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment