Skip to content

Media: Save sub-sizes with non-center crop positions to separate files. - #16144

Open
harmit001 wants to merge 2 commits into
WordPress:trunkfrom
harmit001:40370-crop-position-filenames
Open

harmit001 wants to merge 2 commits into
WordPress:trunkfrom
harmit001:40370-crop-position-filenames

Conversation

@harmit001

@harmit001 harmit001 commented Nov 21, 2026 •

Copy link
Copy Markdown

Summary

Image sub-sizes with the same dimensions but different crop positions are saved to the same file name, so each one overwrites the previous one:

add_image_size( 'newscentered', 400, 400, array( 'center', 'center' ) );
add_image_size( 'newstop', 400, 400, array( 'center', 'top' ) );
add_image_size( 'newsbottom', 400, 400, array( 'center', 'bottom' ) );

All three end up as image-400x400.jpg, and only the last crop survives.

This refreshes the approach from the earlier patches on the ticket (PR #5269 no longer applies to trunk), following comment:6 and comment:7:

  • WP_Image_Editor::get_suffix() appends an 8-character hash of the crop position when a sub-size is cropped from a position other than center, for example image-400x400-1a2b3c4d.jpg.
  • Center crops (crop => true and array( 'center', 'center' )) keep their existing file names, so nothing changes for existing sites, default sizes or plugins that rely on the -{width}x{height} pattern.
  • Crop values are normalized the same way image_resize_dimensions() handles them, so unsupported values (treated as center there) don't produce a hash.
  • The crop position is only set while make_subsize() saves the file in the GD and Imagick editors, and is reset afterwards, so later resize()/save() calls on the same editor are unaffected.

Compared with PR #5269, this:

  • doesn't add a hash key to the image meta,
  • doesn't change file names for center crops,
  • doesn't keep a stale hash on the editor after a cropped sub-size is saved,
  • replaces the test marked FIXME with tests that call multi_resize() directly on both editors.

Testing

New tests: test_multi_resize_with_same_dimensions_and_different_crop_positions in tests/phpunit/tests/image/editorGd.php and editorImagick.php.

npm run test:php -- --filter test_multi_resize_with_same_dimensions_and_different_crop_positions
npm run test:php -- --group image

Manual testing:

  1. Add the three add_image_size() calls above to a theme's functions.php.
  2. Upload a landscape image in Media > Add New.
  3. Before: one -400x400 file in the uploads folder. After: -400x400.jpg plus two -400x400-{hash}.jpg files, each with a different crop.

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

Image sub-sizes with the same dimensions but different crop positions,
for example `add_image_size( 'top', 400, 400, array( 'center', 'top' ) )`
and `add_image_size( 'bottom', 400, 400, array( 'center', 'bottom' ) )`,
were saved to the same file name and overwrote each other.

This appends a short hash of the crop position to the file name suffix
when a sub-size is cropped from a position other than center. Center
crops, including `crop => true`, keep their existing file names.

See #40370.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @harmit001, @[email protected].

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant