Compare Two Images for Similarity Using PHP | PHP Image Comparison

By | January 16, 2017

How to Compare two images for similarity using PHP very well known question and many times we have to face this issue of PHP Image Comparison or Compare Image Similarity using PHP. Many people ask why we need to find the image similarity? before answering this question lets say you want to find your lost son and you have an image of around 5 years back. Now you dont know how he look like so here is the image comparison algorithm which will work for you to search the images which will look like him. I think this is the best example to tell anybody What Image Comparison Class can do. So lets start with our tutorial in this tutorial we have used 4 Files.

  1. 2 Images
  2. Index File (For accessing the class methods)
  3. Image Comparison Class ( image.compare.class.php)





We will start with our Image Comparison Class in which we will use some of our methods which will help us to find the image similarity.

Image.compare.class.php

First of all we will use our mimeType function. This function will tell us the mimeType of the image if its jpeg or png than it will return the mimeType else it will return false.

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;
 }
 }

Now we will use our createImage Function.



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; 
		} 
    }

In the above function we will use the mimType() which will tell us that uploaded image is a jpg or png if the mimeType if different than it will return false otherwise it will call another function.

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;
	}

For resizing the image we will use the above function. It will resize the image in 8*8 square.

We will use the ColorMeanValue Function to takeout the color mean of the image.

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);
	}

Now we will our bits function which will take the colorMeanValue as parameter

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;
	}

After setting all the functions we will use our Image Comparison or Image Similarity function to find out the difference

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;
	}

This is the main function which will compare image similarity by using all the above functions and provide the image comparison value. If the image is less than or lessthan equal to 10 it means the image is almost similar otherwise image is not duplicate.

For using the Image Comparison Class we will use our index file and call our image compare function.

Index File (demo.php)

<?php
require 'image.compare.class.php';
 
/*
	these two images are almost the same so the hammered distance will be less than 10
	try it with images like this:
		1. the example images
		2. two completely different image
		3. the same image (returned number should be 0)
		4. the same image but with different size, even different aspect ratio (returned number should be 0)
	you will see how the returned number will represent the similarity of the images.
*/ 
$class = new compareImages;
echo $class->compare('1.jpg','2.jpg');
  
?>

In the demo.php we will include our Image Comparison class and call the compare function to take out the 2 images similarity.

If you want to download the whole image comparison class and other stuff please use the below url




Compare Two Images for Similarity in PHP

Hope you like our tutorial please comment below if you have any issues. Thank You

One thought on “Compare Two Images for Similarity Using PHP | PHP Image Comparison

Leave a Reply

Your email address will not be published. Required fields are marked *