WordPress.org

Make WordPress Core

Opened 2 months ago

Last modified 7 weeks 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: trunk
Component: Media Keywords:
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.

Change History (2)

#1 @Mista-Flo
7 weeks ago

Can you give steps to reproduce please? And any screenshots if it can help.

#2 @littler.chicken
7 weeks 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.

Note: See TracTickets for help on using tickets.