WordPress.org

Make WordPress Core

Opened 8 weeks ago

Last modified 7 weeks ago

#43930 new defect (bug)

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

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

Description

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:

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

and return:

array(4) {
  [0]=>
  string(70) "http://localhost:8888/wp-content/uploads/2018/05/image.jpg"
  [1]=>
  int(400)
  [2]=>
  int(400)
  [3]=>
  bool(false)
}

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
8 weeks 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:

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

and I added another:

<?php
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:

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

array(4) { [0]=> string(76) "http://local.storelocator.com/wp-content/uploads/2018/05/fclarge-100x100.jpg" [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.

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

array(4) { [0]=> string(78) "http://local.storelocator.com/wp-content/uploads/2018/05/fclarge-2000x1200.jpg" [1]=> int(2000) [2]=> int(1200) [3]=> bool(true) }

and finally, the custom one:

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

array(4) { [0]=> string(76) "http://local.storelocator.com/wp-content/uploads/2018/05/fclarge-300x300.jpg" [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
7 weeks 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.