Skip to content

WIP: add image mask support to media edits#12327

Draft
ramonjd wants to merge 5 commits into
WordPress:trunkfrom
ramonjd:backport/wp-attachment-image-masking
Draft

WIP: add image mask support to media edits#12327
ramonjd wants to merge 5 commits into
WordPress:trunkfrom
ramonjd:backport/wp-attachment-image-masking

Conversation

@ramonjd

@ramonjd ramonjd commented Jun 26, 2026

Copy link
Copy Markdown
Member

In progress.

Trac ticket:

Use of AI Tools


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@ramonjd ramonjd changed the title Add image mask support to media edits WIP: add image mask support to media edits Jun 26, 2026
@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.

unset( $output_format['image/png'] );
return $output_format;
};
add_filter( 'image_editor_output_format', $disable_png_output_mapping, PHP_INT_MAX );

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Masked circle crops must save as PNG to preserve transparent corners.

WordPress allows plugins to globally remap image output formats, for example PNG to WebP. E.g.,

$output_format['image/png'] = 'image/webp';

If that mapping runs here, the code could ask to save a .png mask but the editor might convert it to WebP during save(), which would break the strict “masked output is PNG” expectation.

* }
* @return true|WP_Error True on success, WP_Error object on failure.
*/
public function mask( $args ) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my thinking:

WP_Image_Editor does not need to add mask() if we treat masking as an optional editor capability.

Adding an abstract mask() there would force every third-party image editor subclass to implement it.

That would be a hard backwards compat break, no?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds server-side “mask” support to media edits, enabling REST API clients to request a circular mask and receive a PNG with transparency preserved.

Changes:

  • Adds a mask modifier to the REST attachments edit endpoint, including request schema updates and PNG output handling.
  • Implements mask() support in both WP_Image_Editor_GD and WP_Image_Editor_Imagick, and extends editor capability detection (::test()).
  • Expands PHPUnit coverage: REST edit behavior, editor selection by required methods, and new mock editor support for mask/save spying.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/phpunit/tests/rest-api/rest-attachments-controller.php Adds REST edit tests for mask output and call ordering; strengthens mock reset in teardown.
tests/phpunit/tests/image/editorImagick.php Adds Imagick mask capability and behavioral tests.
tests/phpunit/tests/image/editorGd.php Adds GD mask capability and behavioral tests.
tests/phpunit/tests/image/editor.php Adds tests ensuring editor selection respects required methods (mask).
tests/phpunit/includes/mock-image-editor.php Extends mock editor to spy on save() and adds a mask-capable mock class.
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php Adds mask modifier handling, editor selection constraints, and forces PNG output when masking.
src/wp-includes/media.php Prevents output mime mapping from overriding an explicitly requested output_mime_type.
src/wp-includes/class-wp-image-editor-imagick.php Adds Imagick mask() implementation and capability checks.
src/wp-includes/class-wp-image-editor-gd.php Adds GD mask() implementation and capability checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 813 to +820
if ( is_wp_error( $image_editor ) ) {
if ( $has_mask_modifier ) {
return new WP_Error(
'rest_image_mask_unsupported',
__( 'Unable to mask this image.' ),
array( 'status' => 500 )
);
}
Comment on lines +520 to +529
for ( $y = 0; $y < $height; $y++ ) {
for ( $x = 0; $x < $width; $x++ ) {
$dx = $x - $center_x;
$dy = $y - $center_y;

if ( ( $dx * $dx ) + ( $dy * $dy ) > $radius_squared ) {
imagesetpixel( $this->image, $x, $y, $transparent );
}
}
}
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.

2 participants