travail/image-perceptualhash

travail/image-perceptualhash

v0.1.0 2015-10-09 09:34 UTC

This package is auto-updated.

Last update: 2024-09-14 20:03:56 UTC


README

名称

Image\PerceptualHash - 从图像生成可比较的哈希值

摘要

<?php

use Image\PerceptualHash;
use Image\PerceptualHash\Algorithm\DifferenceHash;
use Image\PerceptualHash\Algorithm\PerceptionHash;

// Create an instance of Image\PerceptualHash, at the same time calculate hashes
// using hashing algorithm Image\PerceptualHash\Algorithm\AverageHash by default
$ph = new PerceptualHash('/path/to/foo.jpg');

// Get binary hash
$binary_hash = $ph->bin();

// Get hexadecimal hash
$hex_hash = $ph->hex();

// Compare with another image, return a Hamming distance
$distance = $ph->compare('/path/to/bar.jpg');

// Calculate a similarity
$similarity = $ph->similarity('/path/to/baz.jpg');

// Calculate by other hashing algorithms
$ph = new PerceptualHash('/path/to/foo.jpg', new DifferenceHash());
// or
$ph = new PerceptualHash('/path/to/foo.jpg', new PerceptionHash());

描述

Image\PerceptualHash 使用三种哈希算法生成独特但非唯一的指纹。与加密哈希不同,这些来自相似图像的指纹也将相似。

依赖关系

  • ext-gd

安装

要通过 composer 将此包安装到您的项目中,请将以下片段添加到您的 composer.json 文件中。然后运行 composer install

"require": {
  "travail/image-perceptualhash": "dev-master"
}

"repositories": {
  {
    "type": "vcs",
    "url": "git@github.com:travail/php-Image-PerceptualHash.git"
  }
}

测试

在克隆项目后,运行 composer install./vendor/bin/phpunit

$ git clone git@github.com:travail/php-Image-PerceptualHash.git
$ cd php-Image-PerceptualHash
$ composer install
$ ./vendor/bin/phpunit

方法

__construct

Image\PerceptualHash __construct($file, Algorithm $algorithm)

创建一个新的 Image\PerceptualHash 实例并计算哈希值。

$file

文件的路径或该资源。

算法 $algorithm

哈希算法,目前有以下算法可用

  • AverageHash
  • DifferenceHash
  • PerceptionHash

bin

string bin()

返回计算的二进制哈希。

hex

string hex()

返回计算的十六进制哈希。

compare

int compare(string|resource $file)

与另一张图像进行比较并返回与该图像的汉明距离。

$file

文件的路径或该资源。

distance

int distance(string $hash1, string $hash2)

similarity

double similarity(string|resource $file)

计算与另一图像的相似度。

$file

文件的路径或该资源。

作者

travail

许可证

此库是自由软件。您可以在 PHP 本身相同的条款下重新分配它或修改它。