简体   繁体   中英

Trying to access array offset on value of type bool after switching to PHP 8.0

Getting the above warning for the following line of code:

switch ( $imagetype[2] ) {

The rest of the code for this below:

function nzshpcrt_display_preview_image() {
global $wpdb;
if ( (isset( $_GET['wpsc_request_image'] ) && ($_GET['wpsc_request_image'] == 'true'))
        || (isset( $_GET['productid'] ) && is_numeric( $_GET['productid'] ))
        || (isset( $_GET['image_id'] ) && is_numeric( $_GET['image_id'] ))
        || (isset( $_GET['image_name'] ))
) {

    if ( function_exists( "getimagesize" ) ) {

        $imagepath   = '';
        $category_id = 0;

        if(isset($_GET['image_name'] )) {
            $image = basename( $_GET['image_name'] );
            $imagepath = WPSC_USER_UPLOADS_DIR . $image;
        } else if ( isset($_GET['category_id'] )) {
            $category_id = absint( $_GET['category_id'] );
            $image = $wpdb->get_var( $wpdb->prepare( "SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `id` = %d LIMIT 1", $category_id ) );
            if ( $image != '' ) {
                $imagepath = WPSC_CATEGORY_DIR . $image;
            }
        }

        if ( ! is_file( $imagepath ) ) {
            $imagepath = WPSC_FILE_PATH . "/images/no-image-uploaded.gif";
        }

        $image_size = @getimagesize( $imagepath );
        if ( is_numeric( $_GET['height'] ) && is_numeric( $_GET['width'] ) ) {
            $height = (int)$_GET['height'];
            $width = (int)$_GET['width'];
        } else {
            $width = $image_size[0];
            $height = $image_size[1];
        }
        if ( !(($height > 0) && ($height <= 1024) && ($width > 0) && ($width <= 1024)) ) {
            $width = $image_size[0];
            $height = $image_size[1];
        }

        $product_id = (int) $_GET ??'' ['productid'];
        $image_id   = (int) $_GET ??'' ['image_id'];

        if ( $product_id > 0 ) {
            $cache_filename = basename( "product_{$product_id}_{$height}x{$width}" );
        } else if ( $category_id > 0 ) {
            $cache_filename = basename( "category_{$category_id}_{$height}x{$width}" );
        } else {
            $cache_filename = basename( "product_img_{$image_id}_{$height}x{$width}" );
        }

        $imagetype = @getimagesize( $imagepath );
        $use_cache = false;
        switch ( $imagetype[2]?? '') {
            case IMAGETYPE_GIF:
                $extension = ".gif";
                break;

            case IMAGETYPE_PNG:
                $extension = ".png";
                break;

            case IMAGETYPE_JPEG:
            default:
                $extension = ".jpg";
                break;

        }
        if ( file_exists( WPSC_CACHE_DIR . $cache_filename . $extension ) ) {
            $original_modification_time = filemtime( $imagepath );
            $cache_modification_time = filemtime( WPSC_CACHE_DIR . $cache_filename . $extension );
            if ( $original_modification_time < $cache_modification_time ) {
                $use_cache = true;
            }
        }

        if ( $use_cache === true ) {
            $cache_url = set_url_scheme( WPSC_CACHE_URL );
            header( "Location: " . $cache_url . $cache_filename . $extension );
            exit( '' );
        } else {
            switch ( $imagetype[2] ) {
                case IMAGETYPE_JPEG:
                    $src_img = imagecreatefromjpeg( $imagepath );
                    $pass_imgtype = true;
                    break;

                case IMAGETYPE_GIF:
                    $src_img = imagecreatefromgif( $imagepath );
                    $pass_imgtype = true;
                    break;

                case IMAGETYPE_PNG:
                    $src_img = imagecreatefrompng( $imagepath );
                    $pass_imgtype = true;
                    break;

                default:
                    $src_img      = false;
                    $pass_imgtype = false;
                    break;
            }

            if ( $pass_imgtype === true && $src_img ) {
                $source_w = imagesx( $src_img );
                $source_h = imagesy( $src_img );

                //Temp dimensions to crop image properly
                $temp_w = $width;
                $temp_h = $height;

                // select our scaling method
                $scaling_method = apply_filters( 'wpsc_preview_image_cropping_method', 'cropping' );

                // set both offsets to zero
                $offset_x = $offset_y = 0;

                // Here are the scaling methods, non-cropping causes black lines in tall images, but doesnt crop images.
                switch ( $scaling_method ) {
                    case 'cropping':
                        // if the image is wider than it is high and at least as wide as the target width.
                        if ( ($source_h <= $source_w ) ) {
                            if ( $height < $width ) {
                                $temp_h = ($width / $source_w) * $source_h;
                            } else {
                                $temp_w = ($height / $source_h) * $source_w;
                            }
                        } else {
                            $temp_h = ($width / $source_w) * $source_h;
                        }
                        break;

                    case 'non-cropping':
                    default:
                        if ( $height < $width ) {
                            $temp_h = ($width / $source_w) * $source_h;
                        } else {
                            $temp_w = ($height / $source_h) * $source_w;
                        }
                        break;
                }

                // Create temp resized image
                $bgcolor_default = apply_filters( 'wpsc_preview_image_bgcolor', array( 255, 255, 255 ) );
                $temp_img = ImageCreateTrueColor( $temp_w, $temp_h );
                $bgcolor = ImageColorAllocate( $temp_img, $bgcolor_default[0], $bgcolor_default[1], $bgcolor_default[2] );
                ImageFilledRectangle( $temp_img, 0, 0, $temp_w, $temp_h, $bgcolor );
                ImageAlphaBlending( $temp_img, true );
                ImageCopyResampled( $temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h );

                $dst_img = ImageCreateTrueColor( $width, $height );
                $bgcolor = ImageColorAllocate( $dst_img, $bgcolor_default[0], $bgcolor_default[1], $bgcolor_default[2] );
                ImageFilledRectangle( $dst_img, 0, 0, $width, $height, $bgcolor );
                ImageAlphaBlending( $dst_img, true );

                // X & Y Offset to crop image properly
                if ( $temp_w < $width ) {
                    $w1 = ($width / 2) - ($temp_w / 2);
                } else if ( $temp_w == $width ) {
                    $w1 = 0;
                } else {
                    $w1 = ($width / 2) - ($temp_w / 2);
                }

                if ( $temp_h < $height ) {
                    $h1 = ($height / 2) - ($temp_h / 2);
                } else if ( $temp_h == $height ) {
                    $h1 = 0;
                } else {
                    $h1 = ($height / 2) - ($temp_h / 2);
                }

                switch ( $scaling_method ) {
                    case 'cropping':
                        ImageCopy( $dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h );
                        break;

                    case 'non-cropping':
                    default:
                        ImageCopy( $dst_img, $temp_img, 0, 0, 0, 0, $temp_w, $temp_h );
                        break;
                }

                $image_quality = wpsc_image_quality();

                ImageAlphaBlending( $dst_img, false );
                switch ( $imagetype[2] ) {
                    case IMAGETYPE_JPEG:
                        header( "Content-type: image/jpeg" );
                        imagejpeg( $dst_img );
                        imagejpeg( $dst_img, WPSC_CACHE_DIR . $cache_filename . '.jpg', $image_quality );
                        @ chmod( WPSC_CACHE_DIR . $cache_filename . ".jpg", 0775 );
                        break;

                    case IMAGETYPE_GIF:
                        header( "Content-type: image/gif" );
                        ImagePNG( $dst_img );
                        ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".gif" );
                        @ chmod( WPSC_CACHE_DIR . $cache_filename . ".gif", 0775 );
                        break;

                    case IMAGETYPE_PNG:
                        header( "Content-type: image/png" );
                        ImagePNG( $dst_img );
                        ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".png" );
                        @ chmod( WPSC_CACHE_DIR . $cache_filename . ".png", 0775 );
                        break;

                    default:
                        $pass_imgtype = false;
                        break;
                }
                exit();
            }
        }
    }
}

}

Any suggestions or help on how to resolve this would be greatly appreciated!

You errors come from getimagesize() here you can find the doc, it can return "false" witch is you case here, cause your image file is impossible to access, I suppose. https://www.php.net/manual/en/function.getimagesize.php

You should add verification on return value getimagesize. Or use try/catch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM