[wp-trac] [WordPress Trac] #38906: wp_get_attachment_image_src() sometimes gives incorrect width and height values

WordPress Trac noreply at wordpress.org
Tue May 17 13:44:46 UTC 2022


#38906: wp_get_attachment_image_src() sometimes gives incorrect width and height
values
--------------------------+-----------------------------
 Reporter:  pressupinc    |       Owner:  (none)
     Type:  defect (bug)  |      Status:  reviewing
 Priority:  normal        |   Milestone:  Future Release
Component:  Media         |     Version:
 Severity:  normal        |  Resolution:
 Keywords:  close         |     Focuses:  administration
--------------------------+-----------------------------
Changes (by antonvlasenko):

 * keywords:  needs-patch => close


Comment:

 == Test Report

 Testing Environment:

 * Browser: Safari 15.4
 * Theme: Twenty Twenty-Two
 * Server: macOS 12.3, PHP 8.0.18 (native), Apache 2.4.53 (native)
 * WordPress: 6.0-alpha-52448-src


 TL;DR

 I can't reproduce this bug.
 The width of the image is limited by the value of the `$content_width`
 global variable.
 There is no bug in the `wp_get_attachment_image_src` function.
 More info can be found here: https://codex.wordpress.org/Content_Width

 == Steps to test

 1. Activate `Twenty Twenty-Two` theme.
 2. Go to the `src/wp-content/themes/twentytwentytwo/functions.php` file
 and add the following code to the very end of the file:

 {{{
 global $content_width;
 $content_width = 2000;
 add_action( 'after_setup_theme', function() {
         add_image_size( 'featured-home', 1600, 600, true );
 } );
 }}}

 3. Go to the Media Library and upload a test image that's exactly `1600` x
 `600` in size.
 4. Go to the database and check the last ID in the `posts` (`wp_posts`)
 table.
 5. Add the following code to the very end of the `src/wp-
 content/themes/twentytwentytwo/functions.php` file (don't forget to
 replace the ID):
 {{{
 $image = wp_get_attachment_image_src( 'replace this value with ID of the
 last post', 'featured-home' );
 }}}
 6. Check the contents of the `$image` array (either with
 [https://www.php.net/manual/en/function.print-r.php print_r] or XDebug).

 === Actual result:
 [[Image(https://cldup.com/_jxDZKLgee.png)]]
 === Expected result:
 It should return an image that is 1600 x 600 in size.
 That's exactly what's happening here, meaning there is no bug.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/38906#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list