syamilmj / Aqua-Resizer

Resize WordPress images on the fly
502 stars 208 forks source link

Notice: Trying to access array offset on value of type bool in #117

Open mazur27 opened 3 years ago

mazur27 commented 3 years ago

I am getting this error on my site.

Notice: Trying to access array offset on value of type bool in /homepages/6/d22556011/htdocs/clickandbuilds/HomeOfficeToGo/wp-content/themes/configurator/framework/plugins/aq_resizer.php on line 115

Notice: Trying to access array offset on value of type bool in /homepages/6/d22556011/htdocs/clickandbuilds/HomeOfficeToGo/wp-content/themes/configurator/framework/plugins/aq_resizer.php on line 116

The code on line 115, 116 is:

            $dst_w = $dims[4];
            $dst_h = $dims[5];

Thanks in advance!

AivahThemes commented 3 years ago

This throws an error in Latest PHP Versions. For PHP versions < 7 you can also use a ternary statement

$dst_w = isset($dims[4]) ? $dims[5] : ''; $dst_h = isset($dims[5]) ? $dims[5] : '';

Garavani commented 3 years ago

Same issue for me. Is this resolved for you? Ok it seems that the above answer resolves it. I wonder why he wrote < 7 instead of > 7, though. Moreover I modified to: "? $dims[4]" in the first statement.

epagecity commented 3 years ago

This throws an error in Latest PHP Versions. For PHP versions < 7 you can also use a ternary statement

$dst_w = isset($dims[4]) ? $dims[5] : ''; $dst_h = isset($dims[5]) ? $dims[5] : '';

should be:

$dst_w = isset($dims[4]) ? $dims[4] : '';
$dst_h = isset($dims[5]) ? $dims[5] : '';
TI-D commented 1 year ago

The above solution posted by @epagecity addresses the "Trying to access array offset on value of type bool" error by checking if the $dims[4] and $dims[5] indices are set before accessing them, using the isset() function. This ensures that the code doesn't attempt to access non-existent array elements and avoids the error. However, $dst_w and $dst_h will be set to empty strings if the respective indices are not set.

I suggest setting $dst_w and $dst_h to the original width and height as it seems more appropriate since it ensures that the variables have valid numeric values. The previous solution sets them to empty strings, which might cause unexpected behavior when these variables are later used in numeric operations.

Instead of the above solution, you could use the following if you want the suggested fallback behavior:

if (is_array($dims)) { $dst_w = $dims[4]; $dst_h = $dims[5]; } else { $dst_w = $orig_w; $dst_h = $orig_h; }

Note: This solution is tested on PHP 8.

Spiritvn commented 11 months ago

The above solution posted by @epagecity addresses the "Trying to access array offset on value of type bool" error by checking if the $dims[4] and $dims[5] indices are set before accessing them, using the isset() function. This ensures that the code doesn't attempt to access non-existent array elements and avoids the error. However, $dst_w and $dst_h will be set to empty strings if the respective indices are not set.

I suggest setting $dst_w and $dst_h to the original width and height as it seems more appropriate since it ensures that the variables have valid numeric values. The previous solution sets them to empty strings, which might cause unexpected behavior when these variables are later used in numeric operations.

Instead of the above solution, you could use the following if you want the suggested fallback behavior:

if (is_array($dims)) { $dst_w = $dims[4]; $dst_h = $dims[5]; } else { $dst_w = $orig_w; $dst_h = $orig_h; }

Note: This solution is tested on PHP 8.

Worked for me, thanks. I also think that fallback to original w/h better than null.

fullmoonlights commented 4 months ago

@Spiritvn I have try your version but then I get error in Line 120 if (is_array($dims)) and Line 134 if ( ! $dims || ( ( ( null === $height && $orig_w == $width ) xor ( null === $width && $orig_h == $height ) ) xor ( $height == $orig_h && $width == $orig_w ) ) ) {

I am fare from understand any of php. how your code match into aq_resizer.php? Below your Code and how I did try to implement it.

            // Get image size after cropping.

/ $dims = image_resize_dimensions( $orig_w, $orig_h, $width, $height, $crop ); $dst_w = $dims[4]; $dst_h = $dims[5]; /
if (is_array($dims)) { $dst_w = $dims[4]; $dst_h = $dims[5]; } else { $dst_w = $orig_w; $dst_h = $orig_h; }

            // 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 {

kind regards

Moon