TP6在php8.2环境报错Implicit conversion from float 23.33333333333333 to int loses precision

php8Implicit int loses precision Implicit conversion from float

TP6部署到PHP8.2版本的服务器上,涉及到图片处理的时候会报如下错误:

a8/845efe6e8438cadf65e6f8efe3234c.png

Implicit conversion from float 477.75 to int loses precision[/********/vendor/topthink/think-image/src/Image.php:270]"

打开Image.php找到如下方法:

/**

     * 裁剪图像

     *

     * @param  integer $w      裁剪区域宽度

     * @param  integer $h      裁剪区域高度

     * @param  integer $x      裁剪区域x坐标

     * @param  integer $y      裁剪区域y坐标

     * @param  integer $width  图像保存宽度

     * @param  integer $height 图像保存高度

     *

     * @return $this

     */

    public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null)

    {

        //设置保存尺寸

        empty($width) && $width   = $w;

        empty($height) && $height = $h;

        do {

            //创建新图像

            $img = imagecreatetruecolor($width, $height);

            // 调整默认颜色

            $color = imagecolorallocate($img, 255, 255, 255);

            imagefill($img, 0, 0, $color);

            //裁剪

            imagecopyresampled($img, $this->im, 0, 0, (int)$x, $y, $width, $height, (int)$w, $h);

            imagedestroy($this->im); //销毁原图

            //设置新图像

            $this->im = $img;

        } while (!empty($this->gif) && $this->gifNext());

        $this->info['width']  = (int) $width;

        $this->info['height'] = (int) $height;

        return $this;

    }

加粗的这一行,在参数前面加上 (int)强制转换一下就好了。