-
Notifications
You must be signed in to change notification settings - Fork 215
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
Fail to generate more than one QR with different gradients #186
Comments
I think going with Consider generating two different QR codes in two different requests but storing them and displaying them again inline – they'd have the same ID if using an instance counter per request. |
I just did some tests: hash('xxh64', 'test')
// = "4fdcca5ddb678139"
hash('xxh128', 'test')
// = "6c78e0e3bd51d358d01e758642b85fb8"
dechex(PHP_INT_MAX)
// = "7fffffffffffffff"
base64_encode(PHP_INT_MAX)
// = "OTIyMzM3MjAzNjg1NDc3NTgwNw==" If you are concerned about size, hex seems to be the way to go (although As I said, up to you, I'll push PR with your choice, just lmk! |
Just as a curiosity, but I've done another (unscientific) test to try performance: // hash xxh64
> $test = ['abc', 'cba', 'test', 'test1', 'abcdefghijklmnopqrstuv'];
$start = microtime(true);
foreach(range(1, 4000000) as $i) {
hash('xxh64', $test[$i%5]);
}
$end = microtime(true);
echo ($end - $start);
// 0.93287110328674
// hash xxh128
$test = ['abc', 'cba', 'test', 'test1', 'abcdefghijklmnopqrstuv'];
$start = microtime(true);
foreach(range(1, 4000000) as $i) {
hash('xxh128', $test[$i%5]);
}
$end = microtime(true);
echo ($end - $start);
// 1.3112859725952
// dechex(random_int())
$start = microtime(true);
foreach(range(1, 4000000) as $i) {
dechex(random_int(PHP_INT_MIN, PHP_INT_MAX));
}
$end = microtime(true);
echo ($end - $start);
// 6.8923921585083 |
xxh64 should be decent enough I'd say :) |
Generating more than one QR with different gradients while using the SVG backend causes all the QRs to render the same gradient as the first QR.
Example code:
Result:
Expected result:
The issue comes from the SVGImageBackend class, when generating the gradient, it gives it an id based on the number of gradients present on the same QR, but if more than one QR is on the same page, the id is the same (
g1
or whatever the number).There can be several ways to fix this, the easiest I can think of is to attach a random number to the id to make sure it is unique (
$id = sprintf('g%d-' . random_int(PHP_INT_MIN, PHP_INT_MAX), ++$this->gradientCount)
), another way could be creating a hash based on the gradient properties (type, start color and end color) and attach it to the id after the count. Something like:XXH128
is supposed to be really fast and 8.1 is now the minimum supported version which added support for it.I'm happy to create a PR in order to fix this, let me know which way you prefer and I'll implement it.
Ps.- Thanks for this great library! 💪
The text was updated successfully, but these errors were encountered: