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

Return value of Jenssegers\ImageHash\Hash::toInt() must be of the type int, float returned #62

Open
ghost opened this issue Aug 27, 2020 · 14 comments

Comments

@ghost
Copy link

ghost commented Aug 27, 2020

Using $hash->toInt() in a image, the returned value is float, not integer

@GrahamCampbell
Copy link
Contributor

I think this only happens on 32bit systems?

@ghost
Copy link
Author

ghost commented Aug 27, 2020

I'm executing it from a Dockerized application over a 64bit Mac.

The container information is:

$ uname --al
Linux php-fpm 4.19.76-linuxkit #1 SMP Tue May 26 11:42:35 UTC 2020 x86_64 GNU/Linux

@GrahamCampbell
Copy link
Contributor

And is the PHP binary 32bit or 64bit?

@ghost
Copy link
Author

ghost commented Aug 28, 2020

Is 64bit, check using this method:

<?php
if (PHP_INT_SIZE==4) {
  echo "32 bits";
}
if (PHP_INT_SIZE==8) {
  echo "64 bits";
}

https://code-boxx.com/check-php-version/

@b1rdex
Copy link

b1rdex commented Sep 2, 2020

toInt signature should be changed to return string instead. It tries to unwrap BigInteger and that leads to unwrapping it to float becuase that's how PHP deals with big numbers that exceeded int range.

@VincentChalnot
Copy link
Contributor

I think you should drop the toInt method and allow access to the BigInteger objet directly, that's what I'm storing in my Doctrine entities with a custom type that unwraps to an unsigned bigint in SQL and it works pretty well.

@aaronflorey
Copy link

Any update to this?

@GrahamCampbell
Copy link
Contributor

This library essentially needs 64bit PHP. As far as I can tell, this issue only occurs on 32-bit builds.

@aaronflorey
Copy link

According to Ghost's snippet, I'm running 64bit PHP. I'm running Ubuntu 20.04, with PHP7.4 from ondrej.

@VincentChalnot
Copy link
Contributor

PHP integers are signed integers, you can't fit a unsigned big integer generated by this hash inside a signed big integer unless you apply some dark magic like this:
VincentChalnot@2e02b0f#diff-4d0b8a3066462343f5ad1a0fb45696e5b321a742cc322fd3188a36c549a39e81R81
I have open a pull request with my fix but it might not pass as it breaks some backward compatibility:
#66

@azurre
Copy link

azurre commented Nov 5, 2021

As a workaround I've replaced
public function toInt(): int
to
public function toInt(): float

and add
ini_set('precision', 30);

@VincentChalnot
Copy link
Contributor

The way floats are store in memory doesn't make sense for this type of algorithm, you can't apply bitwise operations on float and expect them to behave the way integers or binary string do.
Also changing the precision of all floating numbers in the PHP engine seems really overkill.

The ideal way would be to never use numbers but instead only binary strings but you can't apply bitwise operator on strings in MySQL < 8. I think it might be supported in Postgres but I'm not sure.

@4n70w4
Copy link

4n70w4 commented Dec 14, 2021

PHP_INT_SIZE=8

$this->toHex()

ffffffaf01010000

hexdec($this->toHex())

18446743725834043392.0

returned 1.8446743725834E+19

@dddkkk01
Copy link

@4n70w4 same here, just wonder why the same hex value results different decimal values.
looks like it loses 8 bits, so the hamdistance is not accurated.

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

7 participants