WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 2 years ago

#38906 reviewing defect (bug)

wp_get_attachment_image_src() sometimes gives incorrect width and height values

Reported by: pressupinc Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Media Keywords: needs-patch
Focuses: administration Cc:

Description

The following is an example of a problem that happens to me regularly across multiple sites.

I have an image size registered as follows:

add_image_size( 'featured-home', 1600, 600, true ); // width, height, crop

When I run wp_get_attachment_image_src() as follows:

$image = wp_get_attachment_image_src( $post_id, 'featured-home' );

...and then print_r() the result, I get this:

Array
(
    [0] => http://localhost:8080/lacoastalservices/wp-content/uploads/2016/09/wetlands-1600x600.jpg
    [1] => 1080
    [2] => 405
    [3] => 1
)

The image itself is actually 1600 by 600 pixels wide, but for some reason the width and height values given in the array are "scaled down" to the width of the next largest image size on the site (1080px), and the corresponding image height if it were actually that wide (405px).

Note that WordPress's "large" default image size is still at its default of 1024px, so I don't think that's the problem.

You can hopefully reproduce this by running the "Display All Image Sizes" plugin on a few sites and looking for images whose larger image sizes have a mismatch between their identified dimensions and their actual urls. "Display All Image Sizes" is using wp_get_attachment_image_src() to generate the text strings that describe image sizes, which is how I became aware of this bug.

Attachments (1)

mismatch.png (27.3 KB) - added by pressupinc 3 years ago.

Download all attachments as: .zip

Change History (7)

@pressupinc
3 years ago

#1 follow-up: @joemcgill
3 years ago

  • Keywords reporter-feedback added
  • Owner set to joemcgill
  • Status changed from new to reviewing

Thanks for the report @pressupinc and welcome to Trac!

This appears to be an issue with image_constrain_size_for_editor() being applied at an inappropriate time. Can you confirm that you're experiencing this issue on trunk (nightly versions) or if you're on a stable release (e.g., 4.6.1). If this issue is on nightlies, can you reproduce the same issue on previous releases or is this a change in behavior?

Joe

#2 in reply to: ↑ 1 @pressupinc
3 years ago

Replying to joemcgill:

Thanks for the report @pressupinc and welcome to Trac!

This appears to be an issue with image_constrain_size_for_editor() being applied at an inappropriate time. Can you confirm that you're experiencing this issue on trunk (nightly versions) or if you're on a stable release (e.g., 4.6.1). If this issue is on nightlies, can you reproduce the same issue on previous releases or is this a change in behavior?

Joe

This is on 4.6.1. I first remember noticing it 6 months or so ago, so it's been an issue on various stable releases for at least that amount of time.

#3 @joemcgill
3 years ago

  • Keywords needs-patch added; reporter-feedback removed
  • Milestone changed from Awaiting Review to Future Release
  • Owner joemcgill deleted
  • Version trunk deleted

Thanks for the quick feedback @pressupinc. In that case, I believe this is probably related to #34225 as clarifying (or removing) the content width dependency would ultimately fix this issue as well. Since this doesn't seem to be a regression, I'm going to move it to a future release.

#4 @pressupinc
2 years ago

Just wanted to check in on this—is still curtailing the functionality of Display All Image Sizes. I'd be happy to help fix it with a bit of handholding.

Thank you!

Last edited 2 years ago by pressupinc (previous) (diff)

This ticket was mentioned in Slack in #core by pressupinc. View the logs.


2 years ago

This ticket was mentioned in Slack in #core-media by pressupinc. View the logs.


2 years ago

Note: See TracTickets for help on using tickets.