Opened 4 years ago
Last modified 3 years ago
#51058 new defect (bug)
attachment_url_to_postid does not retrieve post ID of really large images
Reported by: | littler.chicken | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.3 |
Component: | Media | Keywords: | has-patch needs-testing |
Focuses: | Cc: |
Description
Since WordPress now sets the _wp_attached_file
meta value for very large images to use the scaled image:
amazing-road-in-pure-nature-picjumbo-com.jpg
becomes
amazing-road-in-pure-nature-picjumbo-com-scaled.jpg
any function which uses attachment_url_to_postid
on a large image URL will not properly retrieve the image's post ID.
Use case: I use this function after removing intermediate image size strings to get the post ID of the uploaded image. I'm currently getting around this issue by inserting -scaled
into the image URL and running the function again if a post ID is not returned, but I would prefer if WP could account for this, please.
Alternatively, if the WordPress function could manage the intermediate size conversion on its own, that would also resolve the issue by removing the need to remove it in the first place.
Attachments (1)
Change History (8)
#2
@
4 years ago
To reproduce, upload a very large image to your site (wider than 2560px). Get the URL of the original uploaded image.
Add code to dump out some information similar to this:
add_filter( 'the_content', function( $content ) { // Enter the URL of the original image var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672.jpg' ) ); // Now add -scaled to the original image name var_dump( attachment_url_to_postid( 'https://nightly.local/wp-content/uploads/2020/03/roman-kraft-197672-scaled.jpg' ) ); return $content; } );
For the first, WordPress will output 0
as no image exists with that filename. The second will return the image ID, because that's the filename assigned to the _wp_attached_file
meta key (ref: https://core.trac.wordpress.org/browser/tags/5.5/src/wp-includes/media.php#L4634).
When I use this function, it's in the context of parsing the RSS feed, at which point the original ID is not available, but I'm retrieving it from the database so that the image can be replaced with a smaller copy. Currently, without a secondary check for the -scaled
image, the replacement fails for very large images.
#3
@
4 years ago
- Version changed from trunk to 5.3
I was able to reproduce this issue.
Appending -scaled
to an oversized PNG images was introduced in 5.3 (https://core.trac.wordpress.org/ticket/48736).
The function attachment_url_to_postid
is simply doing a check against the database with whichever url you give it:
$sql = $wpdb->prepare(
"SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path );
So it makes sense that it'll return nothing since the file was immediately renamed upon upload.
@
3 years ago
Patch to address making attachment_url_to_postid work for all attachment sizes and for permalinks and for "scaled" images
#6
@
3 years ago
I have tested and verified that this patch works for the following URLs on my local environment:
http://wordpress.local/?attachment_id=19
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32.png
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32-scaled.png
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32-1024x869.png
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32-768x652.png
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32-300x255.png
http://wordpress.local/wp-content/uploads/2021/09/Screenshot-from-2021-09-27-15-43-32-150x150.png
Can you give steps to reproduce please? And any screenshots if it can help.