Skip to content

Instantly share code, notes, and snippets.

@ancymathai
Created July 14, 2015 16:49
Show Gist options
  • Save ancymathai/d9f2752c34b78cbf9156 to your computer and use it in GitHub Desktop.
Save ancymathai/d9f2752c34b78cbf9156 to your computer and use it in GitHub Desktop.
image_comparison_using_php
<?php
$a=$_REQUEST['txt1'];
$b=$_REQUEST['txt2'];
class compareImages
{
private function mimeType($i)
{
/*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/
$mime = getimagesize($i);
$return = array($mime[0],$mime[1]);
switch ($mime['mime'])
{
case 'image/jpeg':
$return[] = 'jpg';
return $return;
case 'image/png':
$return[] = 'png';
return $return;
default:
return false;
}
}
private function createImage($i)
{
/*retuns image resource or false if its not jpg or png*/
$mime = $this->mimeType($i);
if($mime[2] == 'jpg')
{
return imagecreatefromjpeg ($i);
}
else if ($mime[2] == 'png')
{
return imagecreatefrompng ($i);
}
else
{
return false;
}
}
private function resizeImage($i,$source)
{
/*resizes the image to a 8x8 squere and returns as image resource*/
$mime = $this->mimeType($source);
$t = imagecreatetruecolor(8, 8);
$source = $this->createImage($source);
imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]);
return $t;
}
private function colorMeanValue($i)
{
/*returns the mean value of the colors and the list of all pixel's colors*/
$colorList = array();
$colorSum = 0;
for($a = 0;$a<8;$a++)
{
for($b = 0;$b<8;$b++)
{
$rgb = imagecolorat($i, $a, $b);
$colorList[] = $rgb & 0xFF;
$colorSum += $rgb & 0xFF;
}
}
return array($colorSum/64,$colorList);
}
private function bits($colorMean)
{
/*returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1*/
$bits = array();
foreach($colorMean[1] as $color){$bits[]= ($color>=$colorMean[0])?1:0;}
return $bits;
}
public function compare($a,$b)
{
/*main function. returns the hammering distance of two images' bit value*/
$i1 = $this->createImage($a);
$i2 = $this->createImage($b);
if(!$i1 || !$i2){return false;}
$i1 = $this->resizeImage($i1,$a);
$i2 = $this->resizeImage($i2,$b);
imagefilter($i1, IMG_FILTER_GRAYSCALE);
imagefilter($i2, IMG_FILTER_GRAYSCALE);
$colorMean1 = $this->colorMeanValue($i1);
$colorMean2 = $this->colorMeanValue($i2);
$bits1 = $this->bits($colorMean1);
$bits2 = $this->bits($colorMean2);
$hammeringDistance = 0;
for($a = 0;$a<64;$a++)
{
if($bits1[$a] != $bits2[$a])
{
$hammeringDistance++;
}
}
return $hammeringDistance;
}
}
$im=new compareImages;
$res=$im->compare($a,$b);
echo "This much bits of difference: ";
echo $res;
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<br>
<img src="<?php echo $a ?>" id="yourImgId" align="left" border="2" width="100" />
<img src="<?php echo $b ?>" id="yourImgId1" align="middle" border="2" width="100"/>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Image Comparison</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="lightergreen">
<hr size=100 width=100% color="lighterblue">
<h1> <center><marquee direction="left">ENTER URL OF THE FORMAT .jpg and .png</marquee></center></h1>
<br>
<form name="form1" method="post" action="image.php">
<center>
<table width="330" border="1">
<tr>
<td><label for="username">Enter first URL</label></td>
<td><input type="text" name="txt1"></center></td>
</tr>
<tr>
<td><label for="username">Enter second URL</label></td>
<td><input type="text" name="txt2"></td>
</tr>
<tr><td></td>
<td> <input type="submit" name="Submit" value="Submit"></td>
</table>
</center>
</form>
<br><br><br><br><br> <br><br><br><br><br> <br><br><br><br><br>
<hr size=100 width=100% color="lighterblue">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment