注意:尝试在第83行上访问类型为bool的值的数组偏移量

问题描述

PHP 7.4中打开调试模式后,刚注意到这一点。有几个相关的问题,但没有一个有投票权的答案。希望为在7.4 WP中也会遇到相同问题的人提供一个不错的答案

这些行显示为:

   // Get image size after cropping.
            $dims = image_resize_dimensions( $orig_w,$orig_h,$width,$height,$crop );
            $dst_w = $dims[4];
            $dst_h = $dims[5];

            // Return the original image only if it exactly fits the needed measures.
            if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {
                $img_url = $url;
                $dst_w = $orig_w;
                $dst_h = $orig_h;
            } else {
                // Use this to check if cropped image already exists,so we can return that instead.
                $suffix = "{$dst_w}x{$dst_h}";
                $dst_rel_path = str_replace( '.' . $ext,'',$rel_path );
                $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";

                if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) {
                    // Can't resize,so return false saying that the action to do Could not be processed as planned.
                    return false;
                }
                // Else check if cache exists.
                elseif ( file_exists( $destfilename ) && getimagesize( $destfilename ) ) {
                    $img_url = "{$upload_url}{$dst_rel_path}-{$suffix}.{$ext}";
                }
                // Else,we resize the image and return the new resized image url.
                else {

                    $editor = wp_get_image_editor( $img_path );

                    if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width,$crop ) ) )
                        return false;

                    $resized_file = $editor->save();

                    if ( ! is_wp_error( $resized_file ) ) {
                        $resized_rel_path = str_replace( $upload_dir,$resized_file['path'] );
                        $img_url = $upload_url . $resized_rel_path;
                    } else {
                        return false;
                    }

                }
            }

解决代码如下:-如批准的答案的注释中所述,声明$ dst_w和dst_h已放入else语句中。

 // Get image size after cropping.
            $dims = image_resize_dimensions( $orig_w,$crop );

            // Return the original image only if it exactly fits the needed measures.
            if ( ! $dims && ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {
                $img_url = $url;
                $dst_w = $orig_w;
                $dst_h = $orig_h;
            } else {
                // Use this to check if cropped image already exists,$rel_path );
                $destfilename = "{$upload_dir}{$dst_rel_path}-{$suffix}.{$ext}";
            $dst_w = $dims[4];
            $dst_h = $dims[5];

                if ( ! $dims || ( true == $crop && false == $upscale && ( $dst_w < $width || $dst_h < $height ) ) ) {
                    // Can't resize,$resized_file['path'] );
                        $img_url = $upload_url . $resized_rel_path;
                    } else {
                        return false;
                    }

                }
            }

解决方法

该通知意味着$ dims不是数组而是布尔值,可能是因为image_resize_dimensions失败并返回false。

如果

image_resize_dimensions返回false,则

  • 原始大小为0以下
  • 目的地和高度均为0 /未指定
  • 原始尺寸等于目标尺寸(在这种情况下,无需调整图像尺寸)

在尝试访问$ dims [4]和$ dims [5]之前,应检查$ dims是否为假。