Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#43930 new defect (bug)

Inaccurate width and height returned when using wp_get_attachment_image_src on the backend.

Reported by: jwoolsch's profile jwoolsch Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.5
Component: Media Keywords:
Focuses: Cc:


I'm using wp_get_attachment_image_src on the backend to do image validation for a custom image selector meta box. When the image is large enough for the requested image size the source of the original image is returned but the dimensions are smaller.

This is my call:

wp_get_attachment_image_src($thumbnail_id, 'custom-image-size');

and return:

array(4) {
  string(70) "http://localhost:8888/wp-content/uploads/2018/05/image.jpg"

The actual image dimensions are 1200x1200.

The image_constrain_size_for_editor call in the image_downsize function seems to be where the problem lies. I can comment out that line and it works as expected.

Change History (2)

#1 @subrataemfluence
6 years ago

  • Keywords reporter-feedback added
  • Version set to 4.9.5

Welcome to Trac and thanks for the ticket!

I tried my hand but could not reproduce the issue at my end with a fresh WordPress 4.9.5 install and twentyseventeen theme activated.

The above theme provides us the following two custom image sizes:

add_image_size( 'twentyseventeen-featured-image', 2000, 1200, true );
add_image_size( 'twentyseventeen-thumbnail-avatar', 100, 100, true );

and I added another:

add_image_size( 'custom-image-size', 300, 300, true );

Then I uploaded an image of dimension 4000x1973

wp_get_attachment_image_src got me the right dimension of image with correct image path. Here are the results:

$thumbnail_id = 175;
wp_get_attachment_image_src($thumbnail_id, 'twentyseventeen-thumbnail-avatar'); // 100 x 100

array(4) { [0]=> string(76) "" [1]=> int(100) [2]=> int(100) [3]=> bool(true) }

as you see the image dimension is only 100x100, which is way small than the one originally uploaded and I am still getting the right file name.

wp_get_attachment_image_src(175, 'twentyseventeen-featured-image'); // 2000 x 1200

array(4) { [0]=> string(78) "" [1]=> int(2000) [2]=> int(1200) [3]=> bool(true) }

and finally, the custom one:

wp_get_attachment_image_src(175, 'custom-image-size'); // 300 x 300

array(4) { [0]=> string(76) "" [1]=> int(300) [2]=> int(300) [3]=> bool(true) }

here also the image dimension is much smaller than the original one but I get the correct file name.

If I have understood the issue correctly and followed the right steps to recreate the issue as you stated, I would like to know if there is any third party plugin activated along with a third party theme. If this is the case I would suggest to use a native theme like twentyseventeen, disable all plugins that you might have an then try to reproduce.

#2 @jwoolsch
6 years ago

  • Keywords reporter-feedback removed

Try uploading the image before you add the custom image size. The error is happening when the requested size doesn't exist and the image data retrieved is of the original upload.

Note: See TracTickets for help on using tickets.