Repository navigation
Add JPEG XL support - #77584
Add JPEG XL support#77584adamsilverstein wants to merge 76 commits into
Conversation
Add client-side JPEG XL support without growing the vips worker bundle. The 3 MB vips-jxl.wasm module is split into its own @wordpress/vips/jxl-wasm script module that is only fetched the first time a JXL image is processed. How it works: - packages/vips/src/jxl-wasm.ts is a tiny new script module whose only job is to export the base64 data URL for vips-jxl.wasm. - @wordpress/vips/jxl-wasm is registered as a new wpScriptModuleExports entry so the build emits build/modules/vips/jxl-wasm.min.js (~3 MB) as a separately loadable module. - vips-worker.ts adds vipsEnsureJxlSupport(), which on first call dynamic-imports '@wordpress/vips/jxl-wasm' to get the data URL, then RPC-passes it to the worker via setJxlWasmUrl(). - The worker (packages/vips/src/index.ts) stores the URL, adds vips-jxl.wasm to dynamicLibraries on the next getVips() call, and returns the URL from locateFile(). If vips was already initialized without JXL, the existing instance is discarded so the reinit picks up JXL support. - packages/upload-media calls vipsEnsureJxlSupport() from prepareItem whenever the input or output type is image/jxl. image/jxl is added to CLIENT_SIDE_SUPPORTED_MIME_TYPES and 'jxl' to VALID_IMAGE_FORMATS and the ImageFormat type. The vipsConvertImageFormat wrapper MIME union is widened accordingly. - JXL encoding uses effort=3 (libvips default 7 is too slow for interactive use). Size impact: - worker.min.js: unchanged (~13.1 MB, same as trunk). - jxl-wasm.min.js: new separate module (~3.0 MB), fetched only when a JXL image is encountered. Alternative to #77570, which bundles vips-jxl.wasm directly into the worker (+3 MB on every editor page load). Opened so the size bot can compare. Refs #76981.
|
Size Change: +850 kB (+10.74%) Total Size: 8.76 MB 📦 View Changed
|
|
Flaky tests detected in 4ce3bac. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/33117672777 Navigates the items list via UP/DOWN arrow keys in
|
Resolve conflict in packages/upload-media/src/store/private-actions.ts: trunk moved image format transcoding from client-side prepareItem to server-driven thumbnail generation. Adopt the new pipeline and keep the JXL WASM lazy-load, but trigger it in two places: prepareItem for JXL input files, and transcodeImageItem for server-requested JXL output.
|
A few notes from a review pass: Bundle size / lighter-weight JXL options (re #76981)
Minor code note
|
JXL is not broadly web-compatible: most browsers (including Chrome) cannot display it and the server cannot read it (GD/Imagick have no JXL decoder, and fileinfo reports it as image/x-jxl). Uploading JXL as-is was rejected outright — by the editor's allowed-MIME check and by core's wp_check_filetype_and_ext() — and even when allowed produced an undisplayable attachment with no dimensions or sub-sizes. Decode JXL to JPEG client-side with vips (the JXL WASM module is already lazy-loaded on demand) and upload the JPEG, mirroring how HEIC is handled. The original .jxl is preserved as a companion file in $metadata['original'] so no data is lost. The editor and front end now use the portable JPEG, with real dimensions and the full set of sub-sizes. - Register image/jxl as an allowed upload MIME type, and restore the type during validation via a magic-byte-checked wp_check_filetype_and_ext filter so the sideloaded original passes despite the image/x-jxl finfo mismatch. - Add original-jxl sideload handling to the REST controller, skipping the dimension read since JXL cannot be measured server-side. - Generalize the HEIC companion delete hook to clean up JXL originals too. - Add an e2e test and JXL asset covering the conversion and companion.
# Conflicts: # packages/upload-media/src/store/private-actions.ts
|
I did some testing on this feature, made some small fixes and got it working, it now properly handles uploaded JXL files. I created a JXL using Squoosh for testing: Because JPEG XL (JXL) is not supported in Chromium where client-side media is active, and generally to provide a web-safe format for users, I decided to treat JXL uploads similar to HEIC uploads. So we do the following:
The output format should also honor the See screencast: jxl.to.jpeg.on.upload.mp4 |
|
I will work on a core backport once the core client-side media feature is restored. |
# Conflicts: # packages/vips/CHANGELOG.md # packages/vips/README.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AcYRm3egtiB9yfX1VFaatb
Without generate_sub_sizes set to false, the server's check for image types it cannot resize rejected the upload before the file type check the test is about could run. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AcYRm3egtiB9yfX1VFaatb
7.2 is in beta, so the core backport targets the next release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AcYRm3egtiB9yfX1VFaatb
# Conflicts: # packages/upload-media/CHANGELOG.md # packages/vips/CHANGELOG.md
Keeping JXL uploads in their own format means the server has to record their width and height and accept their sub-size sideloads, but getimagesize() cannot read JXL. Parse the codestream SizeHeader instead, for the attachment metadata and the sideload endpoint. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
Chrome 155 and Firefox 158 now display JPEG XL, so there is no longer a reason to convert JXL uploads to JPEG and keep the original on the side. Treat JXL like any other vips-supported image: upload it as-is and generate its sub-sizes as JXL on the client. The vips wrappers load JXL support whenever they read or write JXL, holding the worker so a recycle cannot drop it in between. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
The e2e tests asserted the old JPEG conversion and companion original. Check instead that the upload and its sub-sizes are JXL, that HDR and gain-map uploads are stored byte-for-byte, and that transparency carries into the sub-sizes. The test browser cannot display JXL yet, so these tests no longer wait for the image to render. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
|
Browser support for JXL has moved a lot since I wrote #77584 (comment), enough that converting JXL uploads to JPEG no longer makes sense. I've switched this PR to treat JXL like any other image: the upload and all of its sub-sizes stay JXL. Claude pulled together where browsers stand today, and what changed in the PR:
The core backport in WordPress/wordpress-develop#12006 will need the same change. |
The JXL upload_mimes filter ran after core's check_upload_mimes(), so it added JXL back on networks whose upload file types leave it out. Register it first. Also accept untyped arguments in the filetype filter, since core does not type them and some callers pass null, and reuse the codestream reader for the JXL sniff instead of a second copy of the signatures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
The recycle guard only knew about ResizeCrop, Rotate and JXL work, so a transcode, transparency check or UltraHDR probe could have its worker terminated mid-call and never settle. Route every vips wrapper through withVipsWorkerHeld instead of special-casing JXL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
The recipient was only recorded after the RPC resolved, so several JXL operations starting together each copied and transferred the ~3 MB library. Share the pending hand-off, and drop it if it fails so the next call retries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
A failed JXL-enabled init kept the JXL bytes, so every later operation, JPEG and PNG included, retried the broken library. Retired instances were only shut down once no operation at all was running, so a busy batch kept the old WASM heap alive next to the new one. And encodePixelsAsJpeg never held its instance, so it could be shut down under it. Lease an instance per operation instead: each instance counts its own users and shuts down as soon as it is retired and idle. Only JXL work retires a plain instance, and after a failed JXL init other work starts a plain instance while JXL work keeps retrying. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
The test plugin removed JXL at priority 20, after Gutenberg's filter whatever its priority, so it could not catch Gutenberg adding JXL back after check_upload_mimes(). Use the default priority, as core does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
Add a REST test for the sideload endpoint reading a JXL sub-size's dimensions. The empty-container test went through wp_check_filetype_and_ext(), where fileinfo already recognises the JXL ftyp box, so check gutenberg_is_jxl_file() directly instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
The big image threshold check decoded the source with createImageBitmap, which rejects for JXL in browsers without JXL support. The rejection stranded the upload in the queue, so every JXL e2e test timed out. Use the dimensions the server read from the JXL header instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017uBiB3PfgBJpX6dbD1mN4B
# Conflicts: # packages/upload-media/CHANGELOG.md # packages/upload-media/src/store/private-actions.ts
There was a problem hiding this comment.
🟡 Changes recommended
Changes are needed because JXL header parsing can fabricate dimensions from truncated or fragmented data, and JPEG-to-JXL conversion leaves smaller main attachments unchanged when server encoding is unavailable.
3 open issues
What changed in this PR
Adds JPEG XL support to Gutenberg’s client-side media pipeline, loading the decoder only when needed rather than increasing every editor session’s download.
Changes:
- Adds a lazy JXL module and worker lifecycle handling.
- Enables JXL uploads, output formats, and client-generated sub-sizes, with PHP MIME and dimension handling.
- Adds regression coverage, fixtures, and package documentation.
| File | Description |
|---|---|
| tools/eslint/config.mjs | Excludes the standalone fixture generator. |
| test/unit/vitest.config.mjs | Registers worker and JXL module stubs. |
| test/unit/config/worker-code-stub.vitest.ts | Stubs generated worker source. |
| test/unit/config/vips-jxl-wasm-stub.vitest.ts | Stubs JXL decoder bytes. |
| test/e2e/specs/editor/various/client-side-media-processing.spec.js | Tests JXL uploads, fidelity, restrictions, and batches. |
| test/e2e/assets/.gen-jxl-fidelity-fixtures.mjs | Generates JXL fidelity fixtures. |
| phpunit/media/gutenberg-get-jxl-info-test.php | Tests JXL dimensions and attachment metadata. |
| phpunit/media/gutenberg-filter-jxl-filetype-and-ext-test.php | Tests JXL validation and upload restrictions. |
| phpunit/media/class-gutenberg-rest-attachments-controller-test.php | Tests JXL sub-size sideloading. |
| packages/vips/src/worker.ts | Exposes JXL byte delivery through RPC. |
| packages/vips/src/vips-worker.ts | Loads and delivers decoder bytes on demand. |
| packages/vips/src/utils.ts | Enables JXL quality options. |
| packages/vips/src/test/jxl.ts | Tests JXL initialization and instance lifetimes. |
| packages/vips/src/test/ensure-jxl-support.ts | Tests decoder caching, delivery, and retries. |
| packages/vips/src/jxl-wasm.ts | Defines the standalone decoder module. |
| packages/vips/src/index.ts | Adds JXL processing and leased vips instances. |
| packages/vips/README.md | Documents JXL byte-setting APIs. |
| packages/vips/package.json | Exports the JXL script module. |
| packages/vips/CHANGELOG.md | Records on-demand JXL support. |
| packages/upload-media/src/store/utils/test/vips-worker-recycle.ts | Tests recycling protection during active calls. |
| packages/upload-media/src/store/utils/test/jxl-support.ts | Tests conditional JXL loading in wrappers. |
| packages/upload-media/src/store/utils/index.ts | Loads JXL support and protects worker calls. |
| packages/upload-media/src/store/types.ts | Adds JXL format and attachment dimensions. |
| packages/upload-media/src/store/test/private-actions.ts | Tests JXL thumbnails and threshold scaling. |
| packages/upload-media/src/store/test/actions.ts | Tests typed and untyped JXL uploads. |
| packages/upload-media/src/store/private-actions.ts | Routes JXL uploads, formats, and dimension lookup. |
| packages/upload-media/src/store/constants.ts | Marks JXL as client-processable. |
| packages/upload-media/CHANGELOG.md | Records JXL upload support. |
| packages/e2e-tests/plugins/disallow-jxl-uploads.php | Provides an upload-restriction test plugin. |
| lib/media/load.php | Registers JXL and parses its dimensions. |
| lib/media/class-gutenberg-rest-attachments-controller.php | Reads dimensions for sideloaded JXL sub-sizes. |
| backport-changelog/7.3/12006.md | Links the WordPress Core backport. |
Review effort: Balanced
A truncated codestream read missing bits as zero and reported made-up dimensions, and a `jxlp` fragment shorter than the header let the read run into the next box. Gather the header from each partial codestream box's payload, and fail when the parse needs more bits than the file holds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjRxyfBjAYCx2p9Kazo
unpack() can return false, so check its result before using a box size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018vwYjRxyfBjAYCx2p9Kazo
A trunk merge left the entry under the published 5.1.0 heading, which fails the changelog structure check. Claude-Session: https://claude.ai/code/session_018vwYjVtX8TWPbn9AaYEem7
A site that maps image/jxl to image/jpeg through image_editor_output_format got JPEG sub-sizes for every JXL, flattening any transparency. Apply the alpha check PNG already gets, and load JXL support for it so vips can decode the image. Claude-Session: https://claude.ai/code/session_018vwYjVtX8TWPbn9AaYEem7
Its only caller was runVipsCall, so the separate exported helper added nothing. Claude-Session: https://claude.ai/code/session_018vwYjVtX8TWPbn9AaYEem7
Replace the unit tests that mocked the upload pipeline with e2e tests of the same behaviour: an untyped .jxl, an oversized JXL scaled with createImageBitmap unavailable, and output format mapping to and from JXL. Merge the remaining vips instance and worker hand-off tests, which e2e cannot reach. The test browser now renders JXL, so the tests wait for the preview like every other format. One JXL fixture is enough to show the upload is stored byte for byte, so drop the HDR and gain map fixtures and their generator. Claude-Session: https://claude.ai/code/session_018vwYjVtX8TWPbn9AaYEem7


Summary
Adds JPEG XL (JXL) support to client-side media processing, without making every editor page download a JXL decoder it will probably never use.
A JXL upload is treated like any other image the browser can process: the
.jxlitself becomes the attachment, and vips generates its sub-sizes as JXL in the browser, since the server cannot read JXL without GD/Imagick JXL support. Chrome 155 and Firefox 158 display JXL by default and Safari has partial support since 17, so there is no longer a reason to convert it to JPEG the way HEIC is. JXL also becomes an available output format, so an uploaded JPEG/PNG/WebP can be transcoded to JXL on the way in.On size: JXL is not a smaller AVIF. Jake Archibald's comparison (https://jakearchibald.com/2026/jpeg-xl-vs-avif/) finds lossy JXL photos are typically 15-25% larger than the equivalent AVIF at default efforts, and screenshots and illustrations 30-90% larger. libjxl's higher efforts barely help, and JXL is slower to decode. Where JXL does win is lossless images and repacking existing JPEGs 9-20% smaller without further loss, which this PR does not do yet. The case for this PR is that browsers now display JXL, so sites that already have JXL files can upload them, not that JXL saves bytes over AVIF. More detail in #76981.
The JXL WASM decoder (~850 kB gzipped) lives in its own script module chunk that the browser only fetches the first time a JXL image is actually processed. Editor sessions that never touch JXL transfer no extra bytes.
Fixes #76981.
Core backport: WordPress/wordpress-develop#12006
How it works
The lazy chunk.
packages/vips/src/jxl-wasm.tsis a tiny script module that exports the inlined bytes ofwasm-vips/vips-jxl.wasm. It is listed inwpScriptModuleExportsso the build emits a standalonebuild/modules/vips/jxl-wasm.min.js, and@wordpress/vips/workerdeclares it as a dynamicmodule_dependenciesentry so WordPress's import map can resolve it at runtime.Getting the bytes to the worker.
vipsEnsureJxlSupport()on the main thread dynamic-imports that module - which is what triggers the download - and hands the bytes to the worker through a newsetJxlWasm()RPC. It transfers a bareArrayBufferrather than the typed array: comctx only fast-pathsArrayBufferandArrayand walks anything else key by key, which for a multi-megabyte typed array means millions of main-thread allocations per call. The download is cached, and the RPC repeats only when the worker has been recycled since the last call.Worker-side reinit. The worker stores the bytes and adds
vips-jxl.wasmtodynamicLibrarieson the nextgetVips(). If vips was already running without JXL, that instance is shut down first (it holds a large WASM heap that would otherwise be stranded for the life of the worker) and recreated with JXL support.locateFilewraps the bytes in a Blob URL when vips asks forvips-jxl.wasm. JXL encoding useseffort=3- libvips' default of 7 is too slow to sit through interactively.Upload path.
image/jxljoins the vips-processable MIME types, so a JXL takes the same path as a WebP or AVIF: the original is uploaded withgenerate_sub_sizesoff andgenerateThumbnails()sideloads JXL sub-sizes. A.jxlwhoseFile.typecomes back empty, as it does on a lot of systems, is re-labelledimage/jxlfirst. The upload-media vips wrappers (resize, rotate, convert, compress) callvipsEnsureJxlSupport()whenever they read or write JXL, and hold the worker while they do, so a recycle cannot drop the JXL library in between. That also covers transcoding to JXL, where the server picks the output format after the upload response comes back. The big image threshold check reads a JXL's width and height from the server's attachment metadata instead ofcreateImageBitmap(), since browsers without JXL support (including the Chromium used in CI) cannot decode it, and a failed read now skips the scaled version instead of leaving the upload stuck.Server side.
lib/media/load.phpregistersimage/jxlinupload_mimes; core has no JXL entry, so without it the allowed-types check rejects the file. fileinfo reports JXL asimage/x-jxl, so awp_check_filetype_and_extfilter sniffs the magic bytes - both the naked codestream and the ISOBMFF container - and restores the expected extension and type.getimagesize()cannot read JXL either, sogutenberg_get_jxl_info()decodes the width and height from the codestream SizeHeader (swapping them for transposing orientations). Awp_generate_attachment_metadatafilter uses it to record the upload's dimensions, which core needs to report the missing sub-sizes, and the sideload endpoint falls back to it when checking each JXL sub-size.Bundle size
From the size bot on the latest run:
build/modules/vips/jxl-wasm.min.jsbuild/modules/vips/worker.min.jsbuild/scripts/upload-media/index.min.jsThe always-loaded cost is about 720 bytes. The 850 kB is paid once, on demand, and then cached.
Note the chunk is the
vips-jxl.wasmdynamic library inlined into a JS module, so it transfers somewhat heavier than the raw.wasmwould. See the discussion below on lighter-weight alternatives.Screencast
jxl.to.jpeg.on.upload.mp4
Testing instructions
jxl-wasm.min.jsat all..jxlimage.jxl-wasm.min.jsis fetched once, and the block shows the image (in Chrome 155+, Firefox 158+ or Safari 17+; older browsers cannot display JXL)..jxland then another JPEG in the same session. No second fetch, and both process normally on the JXL-enabled vips instance./wp-json/wp/v2/media/<id>should reportmime_typeofimage/jxl, the realwidthandheight, and every entry inmedia_details.sizesshould be a.jxlwithmime_typeofimage/jxl..jxland its sub-sizes are removed from disk.image_editor_output_format) and upload a JPEG. The sub-sizes should be.jxlfiles. The full-size file stays a JPEG, the same as for every other output format in client-side processing.build/modules/vips/worker.min.asset.phpdeclares a dynamicmodule_dependenciesentry on@wordpress/vips/jxl-wasm.Test files: any
.jxlwill do, or200x150_e2e_test_image_decode.jxlintest/e2e/assets/.Tests
client-side-media-processing.spec.js: a JXL upload and its sub-sizes stay JXL with the right dimensions, and the upload is stored byte for byte; a.jxlthe browser reports no MIME type for is still processed; a JXL over the big image threshold gets a-scaled.jxl, withcreateImageBitmapstubbed out to stand in for a browser that cannot decode JXL; a transparent JXL keeps its alpha in the sub-sizes; JXL to JPEG and JPEG to JXL output format mapping convert the sub-sizes, and a transparent JXL keeps JXL sub-sizes when mapped to JPEG; a site that disallows JXL rejects the upload; several JXLs uploaded together alongside a JPEG all process.phpunit/media/gutenberg-get-jxl-info-test.phpcovers the dimension parser (naked codestream,jxlcand splitjxlpcontainers, div8 sizes, orientation, truncated and non-JXL input), andgutenberg-filter-jxl-filetype-and-ext-test.phpthe file type filter's edge cases.packages/vips/src/test/cover what e2e cannot reach: the worker reinit (rebuild with JXL, reuse, recover from a failed init, keep a retired instance alive while in use) andvipsEnsureJxlSupport(download and send once, re-send after a worker recycle, retry a failed download or hand-off).packages/upload-media/src/store/utils/test/vips-worker-recycle.tschecks the worker is not recycled under a vips call in flight.History
This was the third of three approaches tried for #76981, and the one that stuck. The other two are closed:
vips-jxl.wasmstraight into the worker, which added the whole decoder to every editor load whether or not anyone touched a JXL.wp-vips-jxl), which keeps the bytes out of core but puts an install step in front of anyone who wants JXL, and leaves non-admins without it.Splitting the decoder into its own on-demand chunk gets the "no cost unless you use it" property of the plugin approach without the install step.
AI Use
Code and description both written with 🤖 Claude Code. I will review and test.