tecnickcom/tc-lib-barcode

Implicit conversion from float to int loses precision

hkvstore opened this issue · 4 comments

Does not work with PHP 8.1

Type.php, line 544:

imagefilledrectangle($img, $rect[0], $rect[1], $rect[2], $rect[3], $bar_color);
jicho commented

I'm getting the same error when generating QR-codes.
When I change this:

    /**
     * Return Reed-Solomon block code
     *
     * @return array rsblocks
     */
    protected function getCode()
    {
        if ($this->count < $this->dataLength) {
            $row = ($this->count % $this->blocks);
            $col = ($this->count / $this->blocks);
            if ($col >= $this->rsblocks[0]['dataLength']) {
                $row += $this->bv1;
            }
            $ret = $this->rsblocks[$row]['data'][$col];
        } elseif ($this->count < ($this->dataLength + $this->eccLength)) {
            $row = (($this->count - $this->dataLength) % $this->blocks);
            $col = (($this->count - $this->dataLength) / $this->blocks);
            $ret = $this->rsblocks[$row]['ecc'][$col];
        } else {
            return 0;
        }
        ++$this->count;
        return $ret;
    }

into:

    /**
     * Return Reed-Solomon block code
     *
     * @return array rsblocks
     */
    protected function getCode()
    {
        if ($this->count < $this->dataLength) {
            $row = ($this->count % $this->blocks);
            $col = (int) ($this->count / $this->blocks);
            if ($col >= $this->rsblocks[0]['dataLength']) {
                $row += $this->bv1;
            }
            $ret = $this->rsblocks[$row]['data'][$col];
        } elseif ($this->count < ($this->dataLength + $this->eccLength)) {
            $row = (($this->count - $this->dataLength) % $this->blocks);
            $col = (int) (($this->count - $this->dataLength) / $this->blocks);
            $ret = $this->rsblocks[$row]['ecc'][$col];
        } else {
            return 0;
        }
        ++$this->count;
        return $ret;
    }

After that the notice messages are gone in PHP 8.1.

The change was made in .\src\Type\Square\QrCode\Encoder.php (I've added (int) to the $col variable).
QR-codes seems to be working after the change.

@nicolaasuni is there coming a fix for PHP8.1 in the future?

This was fixed by 60dcfc4

jicho commented

Ah.. floor()... never thought about that.

Thanks for fixing this!

In fact the reported issue has not been fixed by 60dcfc4, that commit addressed the same issue in a different place. Please check #77 for the fix of the issue.