Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL DB data type field to store hash for comparision? #54

Open
Ahnungsloser opened this issue Apr 21, 2020 · 1 comment
Open

MySQL DB data type field to store hash for comparision? #54

Ahnungsloser opened this issue Apr 21, 2020 · 1 comment

Comments

@Ahnungsloser
Copy link

Ahnungsloser commented Apr 21, 2020

$hasher = new ImageHash();
$hash = $hasher->hash('test.jpg');

... returns hash in HEX-Format like "3c3e0e1a3a1e1e1e".
tried to convert it: $hash->toBin(), but I'll get fatal error.

Do I have to store the returend hash in the format like in the example (Image 1 hash: 3c3e0e1a3a1e1e1e ), or do I have to convert it to binary format (0011110000111110000011100001101000111010000111100001111000011110)?

Which data type to use for the column to store it in a mysql database for comparision ... varchar(), int()

If I save the hashes in HEX ("3c3e0e1a3a1e1e1e") and try to get the hamming distance, results seems not to be correct.

Results from DB ...
SELECT images.*, BIT_COUNT(hash ^ :hash) as hamming_distance
FROM images
HAVING hamming_distance < 5

... are significantly different from ...
$hasher = new ImageHash(new DifferenceHash());
$distance = $hasher->distance($hash3, $hash4);

@VincentChalnot
Copy link
Contributor

You need to store the hash as an unsigned bigint on MySQL side. (Unfortunately MySQL can't do bitwise operations on varbinary types)
Be careful to keep the number as a string on PHP side because PHP doesn't support unsigned integers and the hash can "overflow" and be cast to float when using the Hash::toInt() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants