Author: Vivek moyal
Updated on: 2017-07-23
Posted on: 1 month ago
Categories: PHP Tutorials
Read this article to learn about this class and how you can use it in PHP applications for checking if two images are the same or not.
How to compare two images for similarity using PHP very well known problem. Many times we have to face this issue of performing PHP image comparison or compare image similarity using PHP. Then you may ask why we need to perform image similarity checks?
Before answering this question lets say you want to find your lost son and you have an image of him about 5 years back. Now you do not know how he looks like. So the image comparison algorithm may work for you to search the images that look like him.
I think this is the best example to tell anybody what the PHP image comparison class can do. In this article I will tell you about another example that you can use this class.
Using this class you can also implement a face recognition based login process. Here is an example how you can achieve it.
At the time of creating the user's account we will store the image of the user to our application storage system. We will save it to the database or a local directory.
Now whenever he wants to attempt the login using face recognition, the application can just get the image using uploading an image from the webcam and use the comparison function for checking for image similarity. If the compare function returns values less than or equal to 10 than you can complete log in process. Otherwise you can show an authentication error message.
So lets start with our tutorial about image comparison. In this tutorial we will use 4 files:
Two images for comparison: 1.jpg and 2.jpg
A processing script for calling the class methods: demo.php
The image comparison class file itself: image.compare.class.php
Image comparison class: image.compare.class.php
First of all we will use our mimeType function. This function will tell us the MIME type of the image. If it is JPEG or PNG than it will return the MIME type. Otherwise it will return false.
function will take an image file name as parameter for checking the extension of the
image file name.
After knowing the MIME type we will resize our image in blocks of 8 x 8 pixels.
resizeImage() takes two parameters. This function takes the MIME type from the source image and calls the createImage function using the image source. The resizeimage function will save the resized image.
The createImage function will check whether the MIME type is of JPEG or PNG. If the condition satisfied than we will call the imagecreatefromjpeg() or imagecreatefrompng(). Otherwise it will just return the false. This function is also used by the resizeImage for creating the resized image.
We will use the colorMeanValue function to take out the color mean value of the image. It will also list the pixels of the colors. This function will return the mean value array.
Now we will our bits function which will take the colorMeanValue array as parameter. This value will take out the bit value in 0,1. If a color value is higher than the mean value, it will be 1 otherwise it will be 0.
After calling all the functions we will use our image comparison or image similarity function to find out the difference. This compare function will take 2 images to compare them, so we have 2 images with us, one is our original image and another one is our resized image.
This function will call the createImage() for both the images and check the return value. When it returns true we will resize both the images and use the image filter for both the images.
Then we will take out the colorMeanValue of both the images and pass the values to the bits function to get the bit value of the images. After getting the bit values we will use the for function and calculate the values of both the images.
If the calculated value difference is less than or equal to 10 than you can say that image is similar, otherwise there is some difference in the image.
This is the main function which will compare image similarity by using all the above functions and provide the image comparison value.
For using the image comparison class we will use our index file and call our image compare function.
The processing script: demo.php
$class = new compareImages;
In the demo.php script we will include our image comparison class and call the compare function to take out the 2 images similarity level value.
If you liked this tutorial article, share it with other PHP developers using the share buttons on this page. If you have other questions or comments post comment here.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
No comments were submitted yet.