I have an image that stored into my database & I need to build a function that give me true or false if the image that sent into the request is the same one or similar to that stored in my database? I need to know if there any package in Laravel App or some way that help me with that
4 Answers
What I understood you need some request validation for process image and compare.this is not the good idea.but you can use: https://www.php.net/manual/en/imagick.compareimages.php i recommend you use it in queue

- 148
- 7
-
thanks for your advice but I need to know is this package work with Laravel 8? – Mohammed_Alkhatib Oct 03 '21 at 13:08
-
yes.imagick is php extenstion and work with laravel 8 – Mohsen Bagheri Oct 03 '21 at 13:17
I have had to do this in a project. Looks like you want to check for almost exact duplicates. https://github.com/jenssegers/imagehash
Used it with Laravel really successfully. I had 40,000 or so images. You have to generate a hash of every image first.
$photo = Photo::find($this->photo_id);
$imageHash = new ImageHash;
$hash = $imageHash->hash($photo->path_thumb);
$photo->comparison_hash = $hash;
$photo->save();
// then to compare
$imageHash = new ImageHash;
$distance = $imageHash->distance($hash,$item->comparison_hash); //$item being another photo
if($distance < $limit) {}//$limit being your similarity threshold

- 750
- 1
- 9
- 22
For php 8.1 and higher and Laravel 8 and higher you can use https://github.com/sapientpro/image-comparator-laravel package. It has Comparator
Facade that wraps main https://github.com/sapientpro/image-comparator class. For example:
use SapientPro\ImageComparatorLaravel\Facades\Comparator;
$similarity = Comparator::compare('path_to_image.jpg', 'path_to_image2.jpg');
echo $similarity; // 100.00

- 71
- 4
Encode your image in base64 and compare with value in database. You don't need any package.

- 146
- 8
-
I think I've made my self clear up there I need to compare the (Content) not the title – Mohammed_Alkhatib Oct 03 '21 at 13:01
-
I talk content not title. Check example below: https://stackoverflow.com/questions/3967515/how-to-convert-an-image-to-base64-encoding – JC Cavalca Oct 03 '21 at 15:50