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. |
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.
Make the image editor mask() operation format-agnostic and move output format selection into the attachments REST edit flow, so masked crops can honor a site's alpha-capable image_editor_output_format (WebP/AVIF) instead of always forcing PNG. - WP_Image_Editor_GD/Imagick::mask() no longer force the mime type to PNG; the caller now saves in an alpha-capable format. Drop the now-unused setImageFormat capability check from Imagick::test() and its test mirror. - edit_media_item() negotiates the output format: honor the site's image_editor_output_format when it can hold an alpha channel and the editor supports it, otherwise fall back to PNG. Derive the extension from the negotiated mime type and suppress the output-format filter during the save so it cannot be remapped back to an opaque format. - Apply mask modifiers after all geometry modifiers (flip/rotate/crop) regardless of request position, and document that contract in the schema. - Add tests covering format negotiation, the opaque-mapping fallback, and mask reordering. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
9205491 to
b1f748f
Compare
The GD and Imagick `test_mask_circle` tests appended `.png` to the path returned by tempnam(), which writes to a different file and leaves the original extension-less temp file behind. Remove that file and reuse the unique path with a `.png` extension, which the editors and Imagick's reader need to infer the format. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Register the save-time `image_editor_output_format` suppression at PHP_INT_MAX instead of 100 so a later site filter on the same hook cannot override it and remap the negotiated alpha-capable format back to an opaque one during save. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add the `mask` modifier to the media edit route schema in the generated QUnit API client fixture (wp-api-generated.js) so it reflects the new modifier. Only the mask entry is added; unrelated fixture drift is excluded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The mask feature is slated for 7.2.0 rather than 7.1.0. Bump the @SInCE tags on the new WP_Image_Editor_GD::mask() and WP_Image_Editor_Imagick::mask() methods, and add @SInCE 7.2.0 change notes to the methods modified to support masking: WP_REST_Attachments_Controller::edit_media_item(), get_edit_media_item_args(), the GD/Imagick test() capability checks, and wp_get_image_editor() (which now honors a caller-specified output_mime_type). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The scanline comment referred to the horizontal reach as `half`, but the code names that value `$span`. Align the comment to avoid a doc/code mismatch. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
In progress.
Backports WordPress/gutenberg#79393
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.