Make WordPress Core

Opened 2 years ago

Last modified 9 months ago

#54943 new defect (bug)

image_get_intermediate_size(): size data is a bool, not an array throws notice / warning

Reported by: fhwebdesign's profile fhwebdesign Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9
Component: Media Keywords: has-patch reporter-feedback needs-testing-info needs-unit-tests changes-requested
Focuses: Cc:

Description (last modified by hellofromTonya)

While testing my website on PHP 8.0 (in my example opening the blog page), I noticed the following two warnings in the error log:

[27-Jan-2022 08:56:34] WARNING: [pool xxx] child 1031078 said into stderr: "PHP message: PHP Warning:  Trying to access array offset on value of type bool in /var/www/xxx/wp-includes/media.php on line 779"
[27-Jan-2022 08:56:34] WARNING: [pool xxx] child 1031078 said into stderr: "PHP message: PHP Warning:  Trying to access array offset on value of type bool in /var/www/xxx/wp-includes/media.php on line 785"

PHP 7.4 throws a Notice.

I also noticed with a fresh install of wordpress.

Change History (9)

#1 @SergeyBiryukov
2 years ago

  • Component changed from General to Media
  • Keywords php8 added
  • Summary changed from PHP Warnings on PHP >= 8 to PHP Warnings in image_get_intermediate_size() on PHP >= 8

#2 @fhwebdesign
21 months ago

JFYI: Issue still exists on 6.0.1

This ticket was mentioned in PR #3143 on WordPress/wordpress-develop by Tabrisrp.

20 months ago

  • Keywords has-patch added; needs-patch removed

The size data is used in the foreach loop without checking if it's an array as expected, and also without checking the existence of the width and height keys.

Also renamed the $data variable to $size_data, as the function already as a $data variable declared before which is holding different values, and it can be confusing.

Trac ticket:

#4 @mariza21
14 months ago

I am also getting this warning error with 8.0 and above.

#5 @hellofromTonya
13 months ago

  • Keywords php80 added; php8 removed

#6 @hellofromTonya
9 months ago

  • Keywords reporter-feedback added
  • Type changed from enhancement to defect (bug)

Hello @fhwebdesign,

Welcome to WordPress Core's Trac! Thank you for reporting the issue you experienced. I'm sorry it's taken so long to welcome you in this ticket.

I'll add some details from your Support Forum report to help contributors investigating this problem:

  • Happens with a cloned WP site, no plugins and using TT2 (Twenty Twenty-Two theme):

    So I cloned my website, deactivated all plugins, switched theme to twenty twentytwo and tried again.

  • Does not happen with a fresh new installation with no images or content.

    A completely new WordPress installation does not throw this warning.
    No, there are no images and pages/posts loaded.

I'm evaluating this ticket to determine if it could be a PHP 8.0 compatibility issue within Core.

The Warning in PHP 8.0 you reported is a Notice in PHP 7.4. The Warning and Notice are alerting developers of a problem.

The code in question expects the 'size' element to be an array as documented in wp_get_attachment_metadata():

 * @return array|false {
 *     Attachment metadata. False on failure.
 *     @type int    $width      The width of the attachment.
 *     @type int    $height     The height of the attachment.
 *     @type string $file       The file path relative to `wp-content/uploads`.
 *     @type array  $sizes      Keys are size slugs, each value is an array containing
 *                              'file', 'width', 'height', and 'mime-type'.
 *     @type array  $image_meta Image metadata.
 *     @type int    $filesize   File size of the attachment.
 * }
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {


I wonder: Why are there no sizes for the attachment(s)? Could the issue be a callback hooked into filter? Or maybe the metadata is incomplete? Or did something happen when the attachment was added or edited?

@fhwebdesign can you share steps of how to reproduce the issue? Does it happen on a specific upload attachment, such as one image? Is this still happening on your website?

Any additional information you can provide helps contributors like me to further investigate to determine if the root cause is in the WordPress Core source code or not.

#7 @hellofromTonya
9 months ago

  • Keywords needs-testing-info needs-unit-tests changes-requested added

Adding keywords for the patch that is attached to this ticket.

#8 @hellofromTonya
9 months ago

Adding historical / contextual references:

During 2.5.0, function image_get_intermediate_size():

  • was added in changeset [7135].
  • The foreach iteration over the $imagedata['sizes'] was added in changeset [7263], which expected the size data to be an array:
    foreach ( $imagedata['sizes'] as $_size => $data ) {
        if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {

The code where the warning / notice is thrown has expected an array of for each size since WP 2.5.0. This code has never had defensive checks to ensure $data is an array before it checks $data['width'].

Thus, I think the root cause is not in image_get_intermediate_size(), but elsewhere.

I'm also wondering if the problem was not introduced in 5.9.0 ???

#9 @hellofromTonya
9 months ago

  • Description modified (diff)
  • Keywords php80 removed
  • Summary changed from PHP Warnings in image_get_intermediate_size() on PHP >= 8 to image_get_intermediate_size(): size data is a bool, not an array throws notice / warning

I've reworded the Summary to better reflect the issue being reported, while focusing it on where the error is being thrown (though likely not the root cause).

I've also removed the php80 keyword. Why? The reported issue is not only related to PHP 8.0. A Notice is thrown on PHP 7. Rather, the issue is: for some reason, there's no size data for one or more attachments.

See the error in action on different PHP versions:

Note: See TracTickets for help on using tickets.