Make WordPress Core

Opened 6 months ago

Closed 8 weeks ago

#65345 closed defect (bug) (fixed)

Site Icon subsizes use wrong array key 'width ' instead of 'width'

Reported by: tomatillo Owned by: davidbaumwald
Priority: normal Milestone: 7.2
Component: Media Version: 4.3
Severity: normal Keywords: has-patch has-unit-tests 2nd-opinion early
Cc: Focuses:

Description (last modified by sabernhardt)

In WP_Site_Icon::additional_sizes(), site icon subsize definitions use the array key 'width ' (with a trailing space) instead of 'width'.

File: wp-admin/includes/class-wp-site-icon.php
Method: WP_Site_Icon::additional_sizes()

The image editor reads $size_data['width']. Because the key is wrong, width is null and subsizes are not generated as intended square crops.

Steps to reproduce:

  1. Set site_icon to a non-square attachment without going through the Customizer crop UI (e.g. assign an existing landscape photo).
  2. Regenerate or create site_icon-* subsizes.
  3. Inspect attachment metadata sizes site_icon-192, site_icon-32, etc.

Expected: 192x192, 32x32 square crops.
Actual: non-square dimensions (e.g. 256x192, 43x32).

Impact: get_site_icon_url() may fall back to the full scaled image for head meta tags.

Fix: change 'width ' to 'width' on the site_icon subsize definition line.

Attachments (3)

65345.patch​ (1.1 KB ) - added by tomatillo 6 months ago.
Fix 'width ' → 'width' in WP_Site_Icon::additional_sizes() so site icon subsizes crop square.
65345.2.patch​ (1.0 KB ) - added by tomatillo 6 months ago.
Supersedes the earlier patch attachment. Please use the latest 65345.patch (Co-authored-by line removed; diff unchanged).
siteicon.png​ (19.5 KB ) - added by shailesh7714 5 months ago.

Download all attachments as: .zip

Change History (31)

@tomatillo
6 months ago

Fix 'width ' → 'width' in WP_Site_Icon::additional_sizes() so site icon subsizes crop square.

#1 @tomatillo
6 months ago

  • Keywords has-patch needs-testing added

Patch attached.

Fixes a trailing space in the site icon subsize definition array key ('width ' → 'width').
Without this, WP_Image_Editor receives a null width and generates non-square site_icon-* crops,
which can cause get_site_icon_url() to fall back to the full scaled image.

Test plan:

  1. Apply patch to trunk.
  2. Assign a non-square attachment as site_icon without the Customizer crop UI.
  3. Regenerate site_icon-* subsizes.
  4. Confirm site_icon-32, site_icon-180, site_icon-192, and site_icon-270 are square.
  5. Confirm get_site_icon_url(192) returns the cropped file, not the full image.

@tomatillo
6 months ago

Supersedes the earlier patch attachment. Please use the latest 65345.patch (Co-authored-by line removed; diff unchanged).

This ticket was mentioned in ​PR #11973 on ​WordPress/wordpress-develop by ​@wprashed.


6 months ago
#2

  • Keywords has-unit-tests added

Fixes a typo in WP_Site_Icon::additional_sizes() where the subsize array used 'width ' (with a trailing space) instead of 'width'.

With the incorrect key, width can be treated as missing during subsize generation, which may produce non-square site_icon-* dimensions in some flows (for example, when assigning a non-square attachment as the site icon outside the Customizer crop UI).

This change restores expected square crops for site icon sizes such as site_icon-32, site_icon-180, site_icon-192, and site_icon-270.

#3 @sabernhardt
6 months ago

  • Description modified (diff)
  • Version 7.0 → 4.3

The array key has been 'width ' since r32994.

​@swissspidy commented on ​PR #11973:


6 months ago
#4

Please note this PR incorrectly includes changes from a bunch of different tickets. A PR should be self-contained and here should only include the site icon change, nothing else.

Also note that the ticket already had a patch, so the extra PR is a bit redundant.

#5 @swissspidy
6 months ago

  • Keywords has-unit-tests removed
  • Milestone Awaiting Review → 7.1

This ticket was mentioned in ​PR #11975 on ​WordPress/wordpress-develop by ​@mohamedahamed.


6 months ago
#6

  • Keywords has-unit-tests added

## Summary

Fixes an issue where site icon subsizes (site_icon-192, site_icon-32, etc.) are generated with incorrect, non-square dimensions instead of the expected square crops (e.g. 192×192, 32×32).

### Root Cause

In ​`src/wp-admin/includes/class-wp-site-icon.php`, the WP_Site_Icon::additional_sizes() method defines subsize entries with the array key 'width ' (containing a trailing space) instead of 'width':

$only_crop_sizes[ 'site_icon-' . $size ] = array(
    'width ' => $size,  // ← trailing space in key
    'height' => $size,
    'crop'   => true,
);

The downstream consumer in ​`src/wp-includes/media.php` (wp_get_registered_image_subsizes()) reads the width using the correct key 'width' (no space):

if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
    $size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
}

Because the keys don't match, the isset() check fails, width remains 0, and the image editor never receives a width constraint. This results in non-square, aspect-ratio-preserved thumbnails instead of the intended square crops.

This typo has existed since the WP_Site_Icon class was introduced in WordPress 4.3.

### Solution

  • Changed 'width ' to 'width' in the subsize definition array inside WP_Site_Icon::additional_sizes().
  • Updated the corresponding test expectations in tests/phpunit/tests/image/siteIcon.php which mirrored the same typo.

## Testing

  1. Set up a local WordPress dev environment (e.g. using wp-env, Local, or Docker).
  2. Upload a non-square image (e.g. a 1200×800 landscape photo) via Media → Add New.
  3. Set it as the site icon without cropping:
    wp option update site_icon <attachment_id>
    
  1. Regenerate the attachment metadata with the site icon filters enabled (this is necessary because by default WP-CLI does not load admin/customizer classes containing site icon filters):
    wp eval '
       require_once ABSPATH . "wp-admin/includes/class-wp-site-icon.php";
       $wp_site_icon = new WP_Site_Icon();
       add_filter( "intermediate_image_sizes_advanced", array( $wp_site_icon, "additional_sizes" ) );
       $metadata = wp_generate_attachment_metadata( <attachment_id>, get_attached_file( <attachment_id> ) );
       wp_update_attachment_metadata( <attachment_id>, $metadata );
       '
    
  1. Inspect the generated subsizes:
    wp post meta get <attachment_id> _wp_attachment_metadata
    
  1. Verify that site_icon-270, site_icon-192, site_icon-180, and site_icon-32 are now square crops (e.g. 192×192, 32×32). Before the fix, they will be non-square dimensions matching the source image's aspect ratio.

---

Trac Ticket: https://core.trac.wordpress.org/ticket/65345

---

Test Case Expected / Result Status
test_additional_sizes — subsizes use 'width' key Array keys match 'width' (no trailing space) ✅ Passed
test_additional_sizes_with_filter — custom sizes use 'width' key Array keys match 'width' (no trailing space) ✅ Passed
Site icon subsizes are square crops after regeneration site_icon-192 = 192×192, site_icon-32 = 32×32, etc. ✅ Passed
get_site_icon_url() returns correct subsize URL Returns subsize URL instead of falling back to full image ✅ Passed

## Use of AI Tools

AI assistance: No

#7 @pedrofigueroa1989
5 months ago

Test Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

Environment

  • WordPress: 7.1-alpha-62161-src
  • Subdirectory: No
  • PHP: 8.3.31
  • Server: nginx/1.31.1
  • Database: mysqli (Server: 9.7.0 / Client: mysqlnd 8.3.31)
  • Browser: Firefox 151.0
  • OS: Ubuntu
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Steps taken

Followed the testing steps from the PR
✅ Patch is solving the problem

Expected result

  • I expected site_icon-270, site_icon-192, site_icon-180, and site_icon-32 to be square crops of the original image.

Screenshots/Screencast with results

'site_icon-270' => 
    array (
      'file' => '383074-New_Zealand-landscape-scaled-270x270.jpg',
      'width' => 270,
      'height' => 270,
      'mime-type' => 'image/jpeg',
      'filesize' => 18642,
    ),
'site_icon-192' => 
    array (
      'file' => '383074-New_Zealand-landscape-scaled-192x192.jpg',
      'width' => 192,
      'height' => 192,
      'mime-type' => 'image/jpeg',
      'filesize' => 11437,
    ),
'site_icon-180' => 
    array (
      'file' => '383074-New_Zealand-landscape-scaled-180x180.jpg',
      'width' => 180,
      'height' => 180,
      'mime-type' => 'image/jpeg',
      'filesize' => 10988,
    ),
'site_icon-32' => 
    array (
      'file' => '383074-New_Zealand-landscape-scaled-32x32.jpg',
      'width' => 32,
      'height' => 32,
      'mime-type' => 'image/jpeg',
      'filesize' => 4025,
    ),
Last edited 5 months ago by pedrofigueroa1989 (previous) (diff)

#8 @agnieszkablu
5 months ago

Test Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

Environment

  • WordPress: 7.1-alpha-62161-src
  • Subdirectory: No
  • PHP: 8.3.31
  • Server: nginx/1.31.1
  • Database: mysqli (Server: 9.7.0 / Client: mysqlnd 8.3.31)
  • Browser: Chrome 148.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Steps taken

Followed the testing steps from the PR

  1. Installed WordPress using wp-env
  2. Uploaded the landscape photo 1200x800
  3. Set up as icon using wp option update site_icon
  4. Regenerated the attachment metadata - using the eval
  5. Verified that icons are square crops:

eg:

'site_icon-192' => 
    array (
      'file' => '0I7A9276-192x192.webp',
      'width' => 192,
      'height' => 192,
      'mime-type' => 'image/webp',
      'filesize' => 8276,
    ),
'site_icon-180' => 
    array (
      'file' => '0I7A9276-180x180.webp',
      'width' => 180,
      'height' => 180,
      'mime-type' => 'image/webp',
      'filesize' => 7622,
    ),
'site_icon-32' => 
    array (
      'file' => '0I7A9276-32x32.webp',
      'width' => 32,
      'height' => 32,
      'mime-type' => 'image/webp',
      'filesize' => 502,
    ),
  ),
  1. ✅ Patch is solving the problem

Expected result

  • The images are cropped to square dimensions.

#9 @jamesbregenzer
5 months ago

Tested PR 11975 locally.

Environment:

  • WordPress 7.1-alpha-62161-src
  • Local wp-env/Docker environment
  • PHP 8.5.6

Steps tested:

  1. Uploaded a non-square image.
  2. Set the uploaded image as the Site Icon from General Settings.
  3. Cropped and saved the Site Icon.
  4. Confirmed the site_icon option was set to the generated cropped attachment.
  5. Inspected the generated attachment metadata.

Result:
The generated site icon sizes are square as expected:

site_icon-270: 270x270
site_icon-192: 192x192
site_icon-180: 180x180
site_icon-32: 32x32

The patch tests successfully for me.

​@yusufmudagal commented on ​PR #11975:


5 months ago
#10

Tested the patch locally against current origin/trunk in a disposable worktree.

Results:

  • php -l src/wp-admin/includes/class-wp-site-icon.php passed.
  • vendor/bin/phpunit --filter Tests_WP_Site_Icon passed: 7 tests, 11 assertions.

The change looks focused to the incorrect width key and the updated Site Icon expectations pass for me.

#11 @darshitrajyaguru97
5 months ago

Test Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

Environment

  • WordPress: 7.1-alpha-62161-src
  • Subdirectory: No
  • PHP: 8.3.31
  • Server: nginx
  • Database: mysql
  • Browser: Chrome
  • OS: Windows 11
  • Theme: Twenty Twenty-Five
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Steps taken

  • Applied the patch from the PR.
  • Followed the testing instructions provided in the PR.
  • Installed WordPress using wp-env.
  • Uploaded a landscape image (1200x800).
  • Set the image as the site icon using wp option update site_icon.
  • Regenerated the attachment metadata using eval.
  • Verified the generated site icon sizes are square crops.

Example verified output:

'site_icon-192' =>
    array (
      'file' => 'test-landscape-icon-192x192.webp',
      'width' => 192,
      'height' => 192,
      'mime-type' => 'image/webp',
      'filesize' => 9134,
    ),

'site_icon-180' =>
    array (
      'file' => 'test-landscape-icon-180x180.webp',
      'width' => 180,
      'height' => 180,
      'mime-type' => 'image/webp',
      'filesize' => 8461,
    ),

'site_icon-32' =>
    array (
      'file' => 'test-landscape-icon-32x32.webp',
      'width' => 32,
      'height' => 32,
      'mime-type' => 'image/webp',
      'filesize' => 621,
    ),

Result

  • Patch successfully resolves the reported issue.
  • Generated site icon images are correctly created with square dimensions.

Expected result

Site icon images should be generated as square crops while preserving the expected icon sizes.

#12 in reply to: ↑ description @shailesh7714
5 months ago

$only_crop_sizes[ 'site_icon-' . $size ] = array(

'width' => $size,
'height' => $size,
'crop' => true,

);

Fixed the issue in the $only_crop_sizes array where the key 'width ' contained an unintended trailing space. This was causing an invalid array key definition.

Updated the code to use the correct key 'width' to ensure proper image size configuration. No functional changes were made beyond this correction.

Thanks

Last edited 5 months ago by shailesh7714 (previous) (diff)

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


4 months ago

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


4 months ago

#15 @davidbaumwald
4 months ago

  • Owner set to davidbaumwald
  • Status new → reviewing

This ticket was mentioned in ​Slack in #core-test by huzaifaalmesbah. ​View the logs.


4 months ago

​@juanmaguitar commented on ​PR #11975:


4 months ago
#17

## Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

### Environment

  • WordPress: 7.1-beta3-62828-src
  • PHP: 8.2.29
  • Server: nginx 1.29.4
  • Database: MySQL 8.4.8
  • OS: macOS (Docker local-env)
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: none
  • Plugins: none

### Steps taken

  1. Uploaded a non-square image (680x1024) and set it as the site icon via wp option update site_icon, skipping the Customizer crop.
  2. Regenerated the attachment metadata so WP_Site_Icon::additional_sizes() ran.
  3. Checked the generated sizes. Before the patch, site_icon-192 came out as 128x192 instead of square.
  4. Applied the patch and repeated the same steps with a fresh upload.
  5. ✅ Patch is solving the problem. site_icon-192 is now 192x192, and the other site_icon sizes are square too.
  6. Ran phpunit --filter Tests_WP_Site_Icon after updating the two test assertions the patch touches: 7 tests, 11 assertions, all green.

### Expected result

Site icon subsizes (site_icon-32, -64, -180, -192, -270) should always be square crops, no matter the source image's aspect ratio.

### Additional Notes

The old tests actually asserted the buggy 'width ' key, so they were passing before the fix without ever catching the bug. Worth knowing this typo's been in core since WP 4.3.

## Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

### Environment

  • WordPress: 7.1-beta3-62828-src
  • PHP: 8.2.29
  • Server: nginx 1.29.4
  • Database: MySQL 8.4.8
  • OS: macOS (Docker local-env)
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: none
  • Plugins: none

### Steps taken

  1. Uploaded a non-square image (680x1024) and set it as the site icon via wp option update site_icon, skipping the Customizer crop.
  2. Regenerated the attachment metadata so WP_Site_Icon::additional_sizes() ran.
  3. Checked the generated sizes. Before the patch, site_icon-192 came out as 128x192 instead of square.
  4. Applied the patch and repeated the same steps with a fresh upload.
  5. ✅ Patch is solving the problem. site_icon-192 is now 192x192, and the other site_icon sizes are square too.
  6. Ran phpunit --filter Tests_WP_Site_Icon after updating the two test assertions the patch touches: 7 tests, 11 assertions, all green.

### Expected result

Site icon subsizes (site_icon-32, -64, -180, -192, -270) should always be square crops, no matter the source image's aspect ratio.

### Additional Notes

The old tests actually asserted the buggy 'width ' key, so they were passing before the fix without ever catching the bug. Worth knowing this typo's been in core since WP 4.3.

### Screenshots/Screencast with results

  • Before: site_icon-192 -> 128x192
  • After: site_icon-192 -> 192x192

### Support Content

Trac ticket: https://core.trac.wordpress.org/ticket/65345

### Screenshots/Screencast with results

  • Before: site_icon-192 -> 128x192
  • After: site_icon-192 -> 192x192

### Support Content

Trac ticket: https://core.trac.wordpress.org/ticket/65345

​@juanmaguitar commented on ​PR #11975:


4 months ago
#18

@NoumaanAhamed Nice catch. One suggestion: test_additional_sizes() just compares the output array to a hardcoded copy with the same typo, so it can never catch this kind of bug. It may be worth adding a test that actually generates attachment metadata for a non-square image and asserts the site_icon-* subsizes come out square — that tests real behavior instead of the array matching itself.

#19 @juanmaguitar
4 months ago

  • Keywords needs-testing removed

#20 @ugyensupport
4 months ago

Test Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/11975

Environment

  • WordPress: 7.0.3-alpha-62793
  • Subdirectory: No
  • PHP: 8.4.14
  • Server: nginx/1.21.4
  • Database: mysqli (Server: 5.7.44-log / Client: mysqlnd 8.4.14)
  • Browser: Chrome 150.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0
    • WordPress Beta Tester 4.0.0


Steps taken

  1. Uploaded a non-square image (680x1024) and set it as the site icon via wp option update site_icon, skipping the Customizer crop.
  2. Regenerated the attachment metadata so WP_Site_Icon::additional_sizes() ran.
  3. Checked the generated sizes. Before the patch, site_icon-192 came out as 128x192 instead of square.
  4. Applied the patch and repeated the same steps with a fresh upload.
  5. ✅ Patch is solving the problem. site_icon-192 is now 192x192, and the other site_icon sizes are square too.

Expected result

  • Site icon subsizes (site_icon-32, -64, -180, -192, -270) should always be square crops, no matter the source image's aspect ratio.

Screenshots/Screencast with results

  • Screenshot/Screencast before site_icon-192 -> 128x192
  • Screenshot/Screencast after site_icon-192 -> 192x192
Last edited 4 months ago by ugyensupport (previous) (diff)

​@wildworks commented on ​PR #11975:


4 months ago
#21

It may be worth adding a test that actually generates attachment metadata for a non-square image and asserts the site_icon-* subsizes come out square, that tests real behavior instead of the array matching itself.

Addressed in ab3661f51d6c43d1337d2885d4cf30d84c310fa4

#22 @wildworks
3 months ago

  • Keywords 2nd-opinion added

​PR 11975 appears to be ready for commit. I am considering fixing this issue in the 7.1 release, but I have concerns about backward compatibility. I believe the impact on developers will likely be minimal, but AI has pointed out the following as potential backward compatibility issues. I would appreciate your feedback.

  • For code applying WP_Site_Icon::additional_sizes() to non-square images, the generated sub-size dimensions change (with a 600x400 source, site_icon-270 goes from 405x270 to 270x270).
  • In the same case, images are now center-cropped, so the sides of a logo get cut off.
  • In the same case, generated filenames change (logo-405x270.png → logo-270x270.png). Implementations that hardcode filenames will 404.
  • Existing saved metadata and files are not migrated, so old and new sub-sizes coexist on a site until a new upload or thumbnail regeneration.
  • Code referencing the 'width ' key (with the trailing space) will break.

#23 @wildworks
3 months ago

  • Milestone 7.1 → 7.2

I'm hesitant to change this now, as the incorrect key has likely been in the core for nearly a decade. Some form of migration might be necessary to ensure that code relying on the incorrect key doesn't break. Let's revisit this ticket in 7.2.

#24 @mukesh27
3 months ago

#65913 was marked as a duplicate.

#25 @analogstudio
3 months ago

As a plugin developer working on media processing, I actually first noticed this issue back in November 2021.

At the time, since this key had already been operating with the trailing space for years (since 4.3), I was honestly torn between using the key as-is to match core or trimming it, and I even hesitated to submit a patch to core out of concern for backward compatibility.

In the end, I decided to implement a custom trim() filter on intermediate_image_sizes_advanced to safely normalize the array keys on my side.

That said, I agree with @wildworks that changing this key should be handled with caution regarding backward compatibility. Fixing this in 7.2 with thorough consideration would be greatly appreciated, as it saves developers from this kind of long-standing confusion and eliminates the need for extra workarounds.

#26 @peterwilsoncc
2 months ago

  • Keywords early added

Putting the early keyword on this given the earlier concerns about doing it in the 7.1 beta period. The good news is that it's _early_ in the 7.2 release cycle now!

#27 @wildworks
8 weeks ago

Yes, I think it's safe to commit now.

#28 @peterwilsoncc
8 weeks ago

  • Resolution → fixed
  • Status reviewing → closed

In 63765:

Media: Ensure cropped site icons generate square images.

Fixes a typo in WP_Site_Icon::additional_sizes() to ensure that sub-sizes generated for site icons are square. A trailing space on the width key could cause the icon to be cropped to rectangles for the sub sizes.

Follow up to r32994.
Developed in ​https://github.com/WordPress/wordpress-develop/pull/11975

Props mohamedahamed, yusufmudagal, agnieszkablu, analogstudio, darshitrajyaguru97, davidbaumwald, jamesbregenzer, juanmaguitar, mukesh27, pedrofigueroa1989, sabernhardt, shailesh7714, swissspidy, tomatillo, ugyensupport, wildworks.
Fixes #65345.

Note: See TracTickets for help on using tickets.