Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mazur27 opened this issue Aug 31, 2020 · 6 comments
Open

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

mazur27 opened this issue Aug 31, 2020 · 6 comments

Comments

@mazur27
Copy link

mazur27 commented Aug 31, 2020

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
Copy link

AivahThemes commented Nov 27, 2020

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
Copy link

Garavani commented Dec 29, 2020

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
Copy link

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
Copy link

TI-D commented Apr 28, 2023

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
Copy link

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
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants