Make WordPress Core

Opened 4 months ago

Closed 3 weeks ago

Last modified 2 weeks ago

#65656 closed defect (bug) (fixed)

Media: Generate animated image sub-sizes by default

Reported by: adamsilverstein Owned by: adamsilverstein
Priority: normal Milestone: 7.2
Component: Media Version:
Severity: normal Keywords: has-patch has-unit-tests gutenberg-merge commit needs-dev-note
Cc: Focuses:

Description (last modified by adamsilverstein)

Updated to reflect the switch from opt-in to opt-out, following the feedback from @peterwilsoncc in comment:11.

Claude rewrote the rest of this description to match the current code:

Background

Resized versions of animated GIFs have always lost their animation: both GD and Imagick flatten animated images when resizing. This is the subject of #28474, open since 2014. It stalled server-side because GD cannot preserve frames at all, Imagick is only available on a subset of hosts, and per-frame re-encoding is costly on shared server CPU.

Client-side media processing (see #64804) changes that calculus. Image decoding, scaling, and encoding happen in the uploading user's browser via wasm-vips, which is available regardless of host configuration and can decode and re-encode every frame of an animated image. The cost is paid once, by the uploader, rather than on the server.

Keeping the animation is what users expect, and flattening it reads as a bug, so this is now the default behavior rather than a developer opt-in.

Proposal

Add a new filter, wp_generate_animated_image_subsizes (boolean, default true). Sites that want the old static behavior, for example to keep upload times down on low powered devices, can opt out with add_filter( 'wp_generate_animated_image_subsizes', '__return_false' );. A one line plugin that does exactly this is available at ​https://github.com/adamsilverstein/disable-animated-subsizes.

The filtered value is exposed on the REST API index as generate_animated_image_subsizes, inside the existing client-side media processing block in WP_REST_Server::get_index() (alongside image_size_threshold and image_max_bit_depth, only for users who can upload_files). The client-side pipeline reads it from there.

Behavior details:

  • By default, every sub-size of an animated GIF or WebP keeps its animation, cropped sizes such as thumbnail included (each frame is cropped with the same rectangle).
  • When an animation is too large to decode in the browser, the pipeline falls back to a static first-frame sub-size rather than failing the upload.
  • Returning false from the filter generates static first-frame sub-sizes, matching the server-side path.
  • Server-side uploads still produce static sub-sizes; the filter currently only affects the client-side path. If server-side animated resizing (per #28474) ever lands for Imagick, the same filter can control it, which is why it lives in core rather than as an editor-only setting.
  • wp_calculate_image_srcset() already declines to mix the full-size animated GIF with its sub-sizes in one srcset. With animated sub-sizes that guard becomes overly conservative but harmless; relaxing it can be a follow-up.

Implementation

The client-side consumption (editor settings plumbing, @wordpress/upload-media, and the @wordpress/vips worker that loads every frame) ships via the Gutenberg package updates in ​https://github.com/WordPress/gutenberg/pull/80385, which has been merged. See ​https://github.com/WordPress/gutenberg/issues/80383 for the design discussion, including the encoder tuning that keeps the cost reasonable.

The core-side change is small: apply the new filter in WP_REST_Server::get_index(), expose the value on the index, and add the field to the block editor preload field lists in wp-admin/edit-form-blocks.php and wp-admin/site-editor.php (these must stay in sync with packages/core-data/src/entities.js for preloading to work).

Core PR: ​https://github.com/WordPress/wordpress-develop/pull/12572

Related

  • #28474 - WordPress destroys animation in animated GIF when it resizes (this resolves it for uploads that go through the client-side path, as proposed in comment 58)
  • #64804 - Client-side media processing

Change History (27)

#1 @adamsilverstein
4 months ago

  • Owner set to adamsilverstein
  • Status new → assigned

This ticket was mentioned in ​PR #12572 on ​WordPress/wordpress-develop by ​@adamsilverstein.


4 months ago
#2

  • Keywords has-unit-tests added; needs-unit-tests removed

Core backport of the server-side changes in ​https://github.com/WordPress/gutenberg/pull/80385 (design discussion in ​https://github.com/WordPress/gutenberg/issues/80383).

Resized versions of animated GIFs have always lost their animation - the subject of the 12-year-old Trac #28474, which stalled server-side because GD cannot preserve frames and Imagick is only available on a subset of hosts. The client-side media processing path can decode and re-encode every frame in the uploading user's browser, independent of host configuration, making animation-preserving sub-sizes feasible as a developer opt-in (they remain too expensive to be the default).

This adds the new wp_generate_animated_image_subsizes filter (boolean, default false) and exposes its value on the REST API index as animated_image_subsizes, inside the existing client-side media processing block in WP_REST_Server::get_index() (alongside image_size_threshold, only for users who can upload_files). When a site opts in, the client pipeline keeps the animation in uncropped sub-sizes of animated GIFs; cropped sizes such as thumbnail always flatten to the first frame.

The field is also added to the block editor root-index preload field lists in wp-admin/edit-form-blocks.php and wp-admin/site-editor.php - these must match packages/core-data/src/entities.js exactly for preloading to work, and the matching entities.js change is part of the Gutenberg PR. (Note: #12508 may want to do the same for image_strip_meta / image_max_bit_depth.)

The client-side consumption (editor settings plumbing, @wordpress/upload-media, and the @wordpress/vips worker restoring the all-frames load path with tuned GIF encoder settings) ships via the Gutenberg package updates in ​https://github.com/WordPress/gutenberg/pull/80385.

---

## Proposed commit message

Media: Add an opt-in filter to generate animated image sub-sizes.

Sub-sizes of animated images have always been static first-frame images: GD and Imagick flatten animated images when resizing, and re-encoding every frame per sub-size is very resource intensive. The client-side media processing path can preserve animation in the uploading user's browser, independent of host Imagick availability.

Add a new wp_generate_animated_image_subsizes filter (boolean, default false) and expose it on the REST API index alongside the other client-side media processing settings, so the client pipeline can honor it for uncropped sub-sizes. The client-side consumption ships via the Gutenberg package updates in https://github.com/WordPress/gutenberg/pull/80385.

Fixes #65656. See #28474, #64804.

#3 @wildworks
4 months ago

  • Keywords gutenberg-merge added
  • Milestone Awaiting Review → 7.1

I would like to change the milestone because this feature might be included in the 7.1 release.

​@adamsilverstein commented on ​PR #12572:


4 months ago
#4

Just noting we have decided to land this in 7.2.

#5 @adamsilverstein
4 months ago

  • Milestone 7.1 → 7.2

I would like to change the milestone because this feature might be included in the 7.1 release.

We have decided to move this to the 7.2 release, see discussion on ​https://github.com/WordPress/gutenberg/pull/80385. I have milestoned as such.

This ticket was mentioned in ​Slack in #core by im3dabasia1. ​View the logs.


6 weeks ago

#7 @im3dabasia1
6 weeks ago

As per today's 7.2 bug scrub:

I see this is confirmed for 7.2. @adamsilverstein confirmed the PR is progressing well, with most of the heavy lifting done on the Gutenberg side. We're optimistic this will land in 7.2.

Thanks, Adam!

​@mahdiali commented on ​PR #12572:


5 weeks ago
#8

Looks good. Clean opt-in approach for preserving animated GIF sub-sizes via the client-side pipeline.

Happy to test once both sides are ready.

#9 @peterwilsoncc
4 weeks ago

If the feature is to be opt-in, then I think it would be better in a plugin.

Sites will need to install a plugin in order to opt-in, that plugin may as well contain the code to animate sub-sizes too.

#10 follow-up: @wildworks
4 weeks ago

If the feature is to be opt-in, then I think it would be better in a plugin.

Sites will need to install a plugin in order to opt-in, that plugin may as well contain the code to animate sub-sizes too.

@peterwilsoncc Could you elaborate on this a bit?

The code that generates animated sub-sizes lives in the editor's client-side media pipeline, which is already part of core and is updated through the Gutenberg package sync. ​https://github.com/WordPress/gutenberg/pull/80385 adds the opt-in path to those packages. ​https://github.com/WordPress/wordpress-develop/pull/12572 only adds the filter that lets a site turn that behavior on.

Because the processing happens inside core's upload pipeline, which has no extension points, a plugin can't realistically ship its own version of it.

The core PR is also needed for preloading: once the packages are synced, the editor requests the new generate_animated_image_subsizes field on the REST API index, so the preload paths in edit-form-blocks.php and site-editor.php must include it. Otherwise the preloaded response no longer matches and is discarded, causing an extra request when the editor loads.

#11 in reply to: ↑ 10 ; follow-up: @peterwilsoncc
4 weeks ago

Replying to wildworks:

@peterwilsoncc Could you elaborate on this a bit?

The code that generates animated sub-sizes lives in the editor's client-side media pipeline, which is already part of core and is updated through the Gutenberg package sync. ​https://github.com/WordPress/gutenberg/pull/80385 adds the opt-in path to those packages. ​https://github.com/WordPress/wordpress-develop/pull/12572 only adds the filter that lets a site turn that behavior on.

More than happy to.

Really, the key phrase is "if the feature is to be opt-in". Anything that's opt-in via a filter requires a plugin anyway, so keeping the other code in the plugin allows for iteration without locking yourselves in to WP's back-compat commitment.

I agree it is a reasonable expectation that animated gifs remain animated when re-sizing.

If y'all are confident in the feature then I'd include a filter to make it opt-out to allow people to turn it off on low powered servers.

#12 @im3dabasia1
4 weeks ago

I’m going through the 7.2 tickets ahead of beta 1.

Thank you for the work on this, @adamsilverstein. I see the core PR is approved and waiting on the Gutenberg PR to merge.

With beta 1 only a few days away, is 7.2 still a realistic target? If it needs more time for discussion, we can move it to 7.3.

#13 in reply to: ↑ 11 @adamsilverstein
3 weeks ago

More than happy to.

Really, the key phrase is "if the feature is to be opt-in". Anything that's opt-in via a filter requires a plugin anyway, so keeping the other code in the plugin allows for iteration without locking yourselves in to WP's back-compat commitment.

I agree it is a reasonable expectation that animated gifs remain animated when re-sizing.

If y'all are confident in the feature then I'd include a filter to make it opt-out to allow people to turn it off on low powered servers.

Thanks @peterwilsoncc!

As I mentioned on the PR, I think this is solid enough to ship as opt-out, and I agree the default behavior should be to preserve animation for sub-sizes when users upload animated images - anything else feels like a bug.

My only concern is performance or user perception when uploading very large GIFs which take some time to process. We may want to introduce a gate for these images and fall back to non animated handling.

With beta 1 only a few days away, is 7.2 still a realistic target? If it needs more time for discussion, we can move it to 7.3.

I do still hope to commit this before beta 1.

​@adamsilverstein commented on ​PR #12572:


3 weeks ago
#14

Looks good. Clean opt-in approach for preserving animated GIF sub-sizes via the client-side pipeline.

Happy to test once both sides are ready.

This is ready for testing @mahdialikhanusiya

​@mahdiali commented on ​PR #12572:


3 weeks ago
#15

@adamsilverstein Thanks I will test it soon. Hopefully by tomorrow.

#16 @adamsilverstein
3 weeks ago

  • Keywords commit added

This worked well in my testing and feels ready for commit ahead of beta 1. The upstream Gutenberg PR was already merged.

Marking for commit.

#17 @adamsilverstein
3 weeks ago

Since I know some people will want to disable this change, I created a one line plugin for this purpose - ​https://github.com/adamsilverstein/disable-animated-subsizes - and submitted it to the plugin repository.

​@adamsilverstein commented on ​PR #12572:


3 weeks ago
#18

Pre-approving this prior to ​WordPress/gutenberg#80385.

Just in case, it might be better to wait to commit this PR until ​WordPress/gutenberg#80385 is merged and confirmed to be shipped in 7.2. Otherwise, a mismatch may occur between the preload list and entities.js.

​https://github.com/WordPress/gutenberg/pull/80385 is merged and I plan to merge this PR ahead of beta 1.

#19 @adamsilverstein
3 weeks ago

@peterwilsoncc - this should be ready for commit before beta 1. If you have, please give it a review. Its already approved, so if you don't have a chance, I'll go ahead and commit and we can always revert it if needed!

#20 @adamsilverstein
3 weeks ago

  • Description modified (diff)
  • Summary Media: add an opt-in filter to generate animated image sub-sizes → Media: Generate animated image sub-sizes by default

​@mahdiali commented on ​PR #12572:


3 weeks ago
#21

Tested on the Playground.

Uploaded an animated GIF via the block editor.

  • Full-size: animates ✅
  • Medium (300×169) and thumbnail (150×150): static (first frame only) ❌

Also tested after changing the resolution in the block editor — sub-sizes still remained static.

Looks like the client-side path may not be generating animated sub-sizes in this environment. Let me know if additional testing steps are needed.

​@adamsilverstein commented on ​PR #12572:


3 weeks ago
#22

@mahdialikhanusiya thanks for testing!

_Claude dug into why the sub-sizes came out static, here is what it found:_

That result is expected on this Playground build. This PR is only the server half: it adds the wp_generate_animated_image_subsizes filter and exposes it on the REST index. The part that actually keeps the frames lives in the editor packages from ​https://github.com/WordPress/gutenberg/pull/80385, which merged on October 16.

wordpress-develop builds the editor from a pinned Gutenberg commit (gutenberg.sha in package.json), currently 954fe7f from September 30, two weeks before that merge. So the Playground for this PR runs the old upload pipeline, which ignores the new field and generates static sub-sizes the same way trunk does.

Animated sub-sizes will show up end to end once the Gutenberg pin in core is bumped past the #80385 merge. Until then, a way to test the full behavior is the Gutenberg plugin from trunk (or a recent nightly) on top of this branch: upload an animated GIF in the block editor, then open the medium and thumbnail files directly, and they should animate.

​@mahdiali commented on ​PR #12572:


3 weeks ago
#23

@adamsilverstein Thanks for the clear explanation! 🙌

#24 @adamsilverstein
3 weeks ago

  • Resolution → fixed
  • Status assigned → closed

In 64490:

Media: Generate animated image sub-sizes by default.

Fix an issue where sub-sizes of animated image uploads were static first-frame images, as GD and Imagick flatten animated images when resizing. When client-side media processing is active, generated images now retain animations.

Add a wp_generate_animated_image_subsizes filter (boolean, default true) so the client pipeline can keep or drop the animation in sub-sizes. The client-side consumption ships via the Gutenberg package updates in ​https://github.com/WordPress/gutenberg/pull/80385.

Fixes #65656, #28474.

#25 @adamsilverstein
2 weeks ago

  • Keywords needs-dev-note added

This one needs a dev note.

​@adamsilverstein commented on ​PR #12572:


2 weeks ago
#26

Dev note draft for this change is in the media dev note comments on #12585: ​https://github.com/WordPress/wordpress-develop/pull/12585#issuecomment-6429238099

Note: See TracTickets for help on using tickets.