Opened 18 months ago
Last modified 4 weeks ago
#58535 new defect (bug)
Undefined array key "file" in function wp_calculate_image_srcset()
Reported by: | martinjhenne | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Media | Keywords: | |
Focuses: | Cc: |
Description
I noticed the following two warnings in the error log:
PHP Warning: Undefined array key "file" in xxx/wp-includes/media.php on line 1367" PHP Warning: Undefined array key "file" in xxx/wp-includes/media.php on line 1373"
Change History (9)
#2
follow-up:
↓ 3
@
18 months ago
- Milestone changed from Awaiting Review to Future Release
- Version 6.2.2 deleted
Hi @martinjhenne,
Thanks for reporting this. It seems like something is causing the metadata associated with your intermediate image sizes (the extra crops that WordPress creates when you upload an image) to be formatted incorrectly. We can probably add some extra safety checks in place to this function in order to avoid these types of warnings, but I would encourage you to find out what is causing this. It could be either a plugin that you are using that modifies the image data before it's saved to the database, or a plugin that is filtering the metadata for image sizes before it reaches this function.
#3
in reply to:
↑ 2
@
18 months ago
Replying to joemcgill:
Hi @martinjhenne,
Thanks for reporting this. It seems like something is causing the metadata associated with your intermediate image sizes (the extra crops that WordPress creates when you upload an image) to be formatted incorrectly. We can probably add some extra safety checks in place to this function in order to avoid these types of warnings, but I would encourage you to find out what is causing this. It could be either a plugin that you are using that modifies the image data before it's saved to the database, or a plugin that is filtering the metadata for image sizes before it reaches this function.
I use svg and webp images. Can this be a reason?
#4
@
2 months ago
I did some digging into this, and here's what I've found:
When Imagick is enabled, and is used to generate thumbnails of PDFs, the metadata associated with the upload does not contain the 'file' key. If someone were to use one of these images in a Post, it would cause this error.
Note that I haven't actually figured out how a "normal" user would get the image URL to use, but at least one of ours has, and that's how I was able to track this down 😅
#5
@
6 weeks ago
Note that I haven't actually figured out how a "normal" user would get the image URL to use, but at least one of ours has, and that's how I was able to track this down
I ran into this today as well and was finally able to figure out how to reproduce it.
- Add a new image block
- Choose "Media Library"
- See the PDF I previously added is not available, select the "Upload" tab
- Upload the PDF
- Confirm by clicking "Select"
Note that if you attempt to upload without going through the media library, the PDF will not be allowed in this context.
At this point, the "Large" version of the PDF, a resized JPG provided by ImageMagick, is used as the image source. But, every time srcset
and sizes
values are calculated, the file
key, which does not exist for the PDF, is not available and a warning is displayed.
I'm not sure why file
is not populated for uploaded PDF files, but adding a bit more defensive check for a missing file
key before processing its value is probably the better move.
And/or... actually disabling PDF uploads through this weird workaround. :)
Related (maybe duplicate): #60480
#6
@
5 weeks ago
This is strange, as there is a check :
$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id ); if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) { return false; }
I went through the core codebase and checked all the instances of the $image_metafile?, and in every function it's checking if the array key file
is defined or not, except one function, and I am working on a patch for it.
Ideally, in wp_calculate_image_srcset
function, this check shouldn't let it pass if there is an undefined array key file
in $image_meta
, @martinjhenne I wonder why you are getting the warnings.
Could this be coming from the $image_meta['sizes']
, which we are looping through and then trying to access the file
of each size?
foreach ( $image_sizes as $image ) { $is_src = false; // Check if image meta isn't corrupted. if ( ! is_array( $image ) ) { continue; } // If the file name is part of the `src`, we've confirmed a match. if ( ! $src_matched && str_contains( $image_src, $dirname . $image['file'] ) ) { $src_matched = true; $is_src = true; }
#7
follow-up:
↓ 8
@
5 weeks ago
I should have noted that I'm seeing this error in wp_image_add_srcset_and_sizes()
, specifically here:
// Bail early if an image has been inserted and later edited. if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] ) ) { return $image; }
I think there's a chance it's happening in more than one place with PDFs, though I think there's a general root cause.
More warnings in the same function