Author: Vivek moyal
Viewers: 3,742
Last month viewers: 538
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.
Contents
Compare Two Images for Similarity Using PHP
Face Recognition in PHP
PHP Compare Images Tutorial
Download and Install the PHP Compare Images Similarity
Compare Two Images for Similarity Using PHP
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.
Face Recognition in PHP
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.
PHP Compare Images Tutorial
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
mimeType()
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.
This
function will take an image file name as parameter for checking the extension of the
image file name.
resizeImage()
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.
createImage()
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.
colorMeanValue()
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.
bits()
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.
compare()
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;
echo $class->compare('1.jpg','2.jpg');
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.
Download and Install the PHP Compare Images Similarity
The PHP Compare Images Similarity class can be downloaded and installed in a ZIP file, or be installed using the PHP Composer tool.
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
1,616,660 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
5. thanks - fbrunello (2020-09-16 12:38)
thanks... - 0 replies
Read the whole comment and replies
4. Not work - Haleem Fyp (2019-10-30 12:59)
.... - 0 replies
Read the whole comment and replies
3. Quickly runs out of memory - Dave Handler (2019-03-15 20:02)
Quickly runs out of memory... - 0 replies
Read the whole comment and replies
2. thank you - bpsim2018 (2019-01-08 19:57)
Thanks... - 0 replies
Read the whole comment and replies
1. tp5er - tp5er (2017-10-18 06:58)
tp5er... - 0 replies
Read the whole comment and replies