{"id":19325196,"url":"https://github.com/sapientpro/image-comparator","last_synced_at":"2025-10-24T21:12:14.883Z","repository":{"id":154133528,"uuid":"627328387","full_name":"sapientpro/image-comparator","owner":"sapientpro","description":"Compare images using PHP","archived":false,"fork":false,"pushed_at":"2025-07-07T12:00:46.000Z","size":9636,"stargazers_count":59,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-04T13:49:20.713Z","etag":null,"topics":["image-comparison","image-hashing","php","php-library","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sapientpro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-13T08:36:16.000Z","updated_at":"2025-09-09T10:53:15.000Z","dependencies_parsed_at":"2024-12-05T23:17:48.868Z","dependency_job_id":"d0f95448-5aa1-4bd2-b873-94fd06044ec8","html_url":"https://github.com/sapientpro/image-comparator","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/sapientpro/image-comparator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapientpro%2Fimage-comparator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapientpro%2Fimage-comparator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapientpro%2Fimage-comparator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapientpro%2Fimage-comparator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sapientpro","download_url":"https://codeload.github.com/sapientpro/image-comparator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sapientpro%2Fimage-comparator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280866398,"owners_count":26404703,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image-comparison","image-hashing","php","php-library","php8"],"created_at":"2024-11-10T02:09:15.214Z","updated_at":"2025-10-24T21:12:14.877Z","avatar_url":"https://github.com/sapientpro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Image Comparator: Compare images using PHP \n\n![https://packagist.org/packages/sapientpro/image-comparator](https://img.shields.io/packagist/dt/sapientpro/image-comparator)\n![https://packagist.org/packages/sapientpro/image-comparator](https://img.shields.io/packagist/v/sapientpro/image-comparator)\n![https://packagist.org/packages/sapientpro/image-comparator](https://img.shields.io/packagist/l/sapientpro/image-comparator)\n\n\u003c!-- TOC --\u003e\n* [Image Comparator: Compare images using PHP](#image-comparator-compare-images-using-php)\n  * [Installation](#installation)\n    * [Prerequisites](#prerequisites)\n  * [Usage](#usage)\n  * [Available methods](#available-methods)\n\u003c!-- TOC --\u003e\n\nImage Comparator is a PHP library for image comparison and hashing.\nYou can compare 2 and more images using perceptual hashing method.\n\nBased on https://github.com/kennethrapp/phasher package, with PHP 8 and PHPUnit support.\nThe original project was abandoned in November 2017.\n\nPerceptual hashing is a method to generate a hash of an image which allows multiple images to be compared by an index of similarity.\nYou can find out more at [The Hacker Factor](http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html)\nand [phash.org](http://phash.org).\n\n## Installation\n\n### Prerequisites\n\n* PHP 8.1 or higher\n* gd extension enabled\n\nTo install the library, run:\n\n`composer require sapientpro/image-comparator`\n\n## Usage\n\n`ImageComparator` is the core class of the library:\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$imageComparator = new ImageComparator();\n```\n\nAfter creating an instance you can use one of the methods available:\n\n```php\n$imageComparator-\u003ecompare('your-images/your-image1.jpg', 'your-images/your-image12.jpg');\n```\n\nIf the image path can't be resolved, `ImageResourceException` will be thrown:\n\n```php\n$imageComparator-\u003ehashImage('your-images/non-existent-image.jpg'); // SapientPro\\ImageComparator\\ImageResourceException: Could not create an image resource from file\n```\n\nExample usage:\n\nWe have two images:\n\nhttps://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image.png?raw=true\n\n![Equals1](https://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image.png?raw=true)\n![Equals2](https://github.com/sapientpro/image-comparator/blob/master/tests/images/amazon-image.png?raw=true)\n\nNow, let's compare them:\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/ebay-image.png?raw=true';\n$image2 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/amazon-image.png?raw=true';\n\n$imageComparator = new ImageComparator();\n$similarity = $imageComparator-\u003ecompare($image1, $image2); //default hashing without rotation\n\necho $similarity; //87.5\n```\nThe higher the result, the higher the similarity of images.\n\nLet's compare different images:\n\n![Equals1](https://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image2.png?raw=true)\n![Equals2](https://github.com/sapientpro/image-comparator/blob/master/tests/images/amazon-image2.png?raw=true)\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/ebay-image2.png?raw=true';\n$image2 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/amazon-image2.png?raw=true';\n\n$imageComparator = new ImageComparator();\n$similarity = $imageComparator-\u003ecompare($image1, $image2); //default hashing without rotation\n\necho $similarity; //54.7\n```\n\nRotation angle can be passed if compared image is rotated.\nYou must pass \\SapientPro\\ImageComparator\\Enum\\ImageRotationAngle enum with one of the following values:\n`D0` = 0 degress, `D90` = 90 degrees, `D180` = 180 degrees, `D270` = 270 degrees\n\n```php\nuse SapientPro\\ImageComparator\\Enum\\ImageRotationAngle;\n\n$similarity = $imageComparator-\u003ecompare($image1, $image2, ImageRotationAngle::D180); //compared image will be considered rotated by 180 degrees\n\necho $similarity; //95.3\n```\n\nYou can also use `detect()` method which will rotate the compared image and return the highest percentage of similarity:\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';\n$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';\n\n$imageComparator = new ImageComparator();\n$similarity = $imageComparator-\u003edetect($image1, $image2);\n\necho $similarity; //95.3\n```\n\nWith `compareArray()` and `detectArray()` methods you can compare the source image to any number of images you want.\nThe behaviour is the same as in `compare()` and `detect()` methods.\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';\n$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';\n$image3 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest.jpg';\n\n$imageComparator = new ImageComparator();\n$similarity = $imageComparator-\u003ecompareArray(\n    $image1,\n    [\n        'forest' =\u003e $image2,\n        'anotherForest' =\u003e $image3\n    ]\n); // returns ['forest' =\u003e 95.33, 'anotherForest' =\u003e 75.22]\n```\n\nIf needed, you can create a square image resource from another image\nand pass it to `compare()`, `compareArray()`, `detect()`, `detectArray` and `hashImage()` methods:\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';\n$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';\n\n$imageComparator = new ImageComparator();\n\n$squareImage1 = $imageComparator-\u003esquareImage($image1);\n$squareImage2 = $imageComparator-\u003esquareImage($image2);\n\n$similarity = $imageComparator-\u003ecompare($squareImage1, $squareImage2);\n\necho $similarity //96.43;\n```\n\nIf needed, you can convert the resulting array from `hashImage()` to a binary string and pass it to `compareHashStrings()` method:\n\n```php\nuse SapientPro\\ImageComparator\\ImageComparator;\n\n$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';\n$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';\n\n$imageComparator = new ImageComparator();\n\n$hash1 = $imageComparator-\u003ehashImage($image1);\n$hash2 = $imageComparator-\u003ehashImage($image2);\n\n$hashString1 = $imageComparator-\u003econvertHashToBinaryString($hash1);\n$hashString2 = $imageComparator-\u003econvertHashToBinaryString($hash2);\n\n$similarity = $imageComparator-\u003ecompareHashStrings($hashString1, $hashString2);\n\necho $similarity //96.43;\n```\n\nBy default, images are hashed using the average hashing algorithm,\nwhich is implemented in `SapientPro\\ImageComparator\\Strategy\\AverageHashStrategy`.\nThis strategy is initialized in the constructor of `ImageComparator`.\n\nIt is also possible to use difference hashing algorithm, implemented in SapientPro\\ImageComparator\\Strategy\\DifferenceHashStrategy.\nTo use it, you need to call ImageComparator's `setHashStrategy()` method and pass the instance of the strategy:\n\n```php\nuse SapientPro\\ImageComparator\\Strategy\\DifferenceHashStrategy;\n\n$imageComparator-\u003esetHashStrategy(new DifferenceHashStrategy());\n$imageComparator-\u003ehashImage('image1.jpg');\n```\n\nIf the strategy is set by `setHashingStrategy()`, it will be used under the hood in other comparison methods:\n\n```php\nuse SapientPro\\ImageComparator\\Strategy\\DifferenceHashStrategy;\n\n$imageComparator-\u003esetHashStrategy(new DifferenceHashStrategy());\n$imageComparator-\u003ecompare('image1.jpg', 'image2.jpg'); // images will be hashed using difference hash algorithm and then compared\n```\n\n## Available methods\n\nYou can read about available methods in our [wiki page](https://github.com/sapientpro/image-comparator/wiki)\n\nDeveloped by SapientPro Team - [Custom software development](https://sapient.pro)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsapientpro%2Fimage-comparator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsapientpro%2Fimage-comparator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsapientpro%2Fimage-comparator/lists"}