WIP: add image mask support to media edits#12327
Conversation
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to 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, |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| unset( $output_format['image/png'] ); | ||
| return $output_format; | ||
| }; | ||
| add_filter( 'image_editor_output_format', $disable_png_output_mapping, PHP_INT_MAX ); |
There was a problem hiding this comment.
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 ) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
maskmodifier to the REST attachments edit endpoint, including request schema updates and PNG output handling. - Implements
mask()support in bothWP_Image_Editor_GDandWP_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.
| 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 ) | ||
| ); | ||
| } |
| 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 ); | ||
| } | ||
| } | ||
| } |
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.