Skip to content

WIP: add image mask support to media edits - #12327

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

WIP: add image mask support to media edits#12327
ramonjd wants to merge 12 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.

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.

@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.

Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php Outdated
Comment thread src/wp-includes/class-wp-image-editor-imagick.php

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 thread src/wp-includes/class-wp-image-editor-gd.php

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

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

Comment thread src/wp-includes/class-wp-image-editor-imagick.php
Comment thread tests/phpunit/tests/image/editorImagick.php
ramonjd and others added 6 commits July 15, 2026 15:36
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>
@ramonjd
ramonjd force-pushed the backport/wp-attachment-image-masking branch from 9205491 to b1f748f Compare July 15, 2026 05:59
@ramonjd
ramonjd requested a review from Copilot July 15, 2026 06:01
Comment thread src/wp-includes/class-wp-image-editor-gd.php

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

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

Comment thread tests/phpunit/tests/image/editorImagick.php Outdated
Comment thread tests/phpunit/tests/image/editorGd.php Outdated
ramonjd and others added 6 commits July 15, 2026 16:14
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>
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