0

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 Answers4

1

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

1

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
BobB
  • 750
  • 1
  • 9
  • 22
1

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
-1

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

JC Cavalca
  • 146
  • 8