diff --git a/docs/prd/conversion-engine.md b/docs/prd/conversion-engine.md index e5de496..c06dfb2 100644 --- a/docs/prd/conversion-engine.md +++ b/docs/prd/conversion-engine.md @@ -39,7 +39,7 @@ Prefer the original file when output would be larger (unless forced), on unsuppo - MIME set filterable via `cimo.imageConverter.mimeTypes` - Prepare hook: `cimo.convertImage.prepare` (Premium HEIC → PNG) - Optimize hook: `cimo.imageConverter.optimize` (Premium Smart Optimization); else `convert()` -- Animated GIF detection: do not corrupt animations +- Animated GIFs convert to animated WebP, and animated WebP uploads are optimized - Progress: support determinate progress for UI when conversion is non-trivial ### Premium converters (via filters) diff --git a/e2e/fixtures/sample-animated-transparent.gif b/e2e/fixtures/sample-animated-transparent.gif new file mode 100644 index 0000000..d65d29f Binary files /dev/null and b/e2e/fixtures/sample-animated-transparent.gif differ diff --git a/e2e/fixtures/sample-animated.gif b/e2e/fixtures/sample-animated.gif new file mode 100644 index 0000000..1842593 Binary files /dev/null and b/e2e/fixtures/sample-animated.gif differ diff --git a/e2e/fixtures/sample-animated.webp b/e2e/fixtures/sample-animated.webp new file mode 100644 index 0000000..a20ee4f Binary files /dev/null and b/e2e/fixtures/sample-animated.webp differ diff --git a/e2e/fixtures/sample-still.gif b/e2e/fixtures/sample-still.gif new file mode 100644 index 0000000..a623c9e Binary files /dev/null and b/e2e/fixtures/sample-still.gif differ diff --git a/e2e/readme.md b/e2e/readme.md index f2be30c..ac4dedb 100644 --- a/e2e/readme.md +++ b/e2e/readme.md @@ -119,6 +119,7 @@ WP_PASSWORD=password | General | Thumbnail generation on/off (intermediate sizes) | | Quality | Lower WebP quality → smaller file than higher quality | | Already-WebP | Re-upload stays `image/webp` | +| Animated WebP | GIF → animated WebP; still GIF → still WebP; animated WebP stays animated; transparent GIF keeps alpha | | Media Library grid | Drop → WebP | | Media modal | Select Files → WebP | | PNG / multi-file | Convert to WebP | diff --git a/e2e/test-utils/index.ts b/e2e/test-utils/index.ts index 7cfda26..a9bb5b8 100644 --- a/e2e/test-utils/index.ts +++ b/e2e/test-utils/index.ts @@ -10,6 +10,10 @@ export { SAMPLE_OVERSIZED_JPG, SAMPLE_SVG, SAMPLE_HEIC, + SAMPLE_STILL_GIF, + SAMPLE_ANIMATED_GIF, + SAMPLE_ANIMATED_WEBP, + SAMPLE_ANIMATED_TRANSPARENT_GIF, dropFile, dropFiles, waitForCimoReady, @@ -25,6 +29,8 @@ export { uploadSampleViaMediaNew, waitForOptimizedUploadMeta, fetchUrlByteLength, + fetchUrlBytes, + isAnimatedWebp, openPopupAndGetByteLength, getMediaFileByteLength, expectCimoSidebarStats, diff --git a/e2e/test-utils/media.ts b/e2e/test-utils/media.ts index 9164177..bfc2f98 100644 --- a/e2e/test-utils/media.ts +++ b/e2e/test-utils/media.ts @@ -44,6 +44,30 @@ export const SAMPLE_SVG = path.resolve( __dirname, '../fixtures/sample.svg' ) /** HEIC fixture for premium HEIC → WebP upload tests. */ export const SAMPLE_HEIC = path.resolve( __dirname, '../fixtures/sample.heic' ) +/** Single-frame GIF for still-image conversion. */ +export const SAMPLE_STILL_GIF = path.resolve( + __dirname, + '../fixtures/sample-still.gif' +) + +/** Two-frame photographic GIF for animated WebP conversion. */ +export const SAMPLE_ANIMATED_GIF = path.resolve( + __dirname, + '../fixtures/sample-animated.gif' +) + +/** Two-frame animated WebP for re-optimization. */ +export const SAMPLE_ANIMATED_WEBP = path.resolve( + __dirname, + '../fixtures/sample-animated.webp' +) + +/** Two-frame GIF with transparent margins for alpha-preserving conversion. */ +export const SAMPLE_ANIMATED_TRANSPARENT_GIF = path.resolve( + __dirname, + '../fixtures/sample-animated-transparent.gif' +) + type DropFileSpec = { path: string; mimeType?: string; @@ -410,6 +434,46 @@ export async function fetchUrlByteLength( page: Page, url: string ) { }, url ) } +/** + * Same-origin file bytes for container checks (animated WebP ANIM/VP8X). + */ +export async function fetchUrlBytes( page: Page, url: string ) { + const data = await page.evaluate( async ( targetUrl ) => { + const response = await fetch( targetUrl, { credentials: 'same-origin' } ) + if ( ! response.ok ) { + throw new Error( `Failed to fetch ${ targetUrl }: ${ response.status }` ) + } + return Array.from( new Uint8Array( await response.arrayBuffer() ) ) + }, url ) + + return Buffer.from( data ) +} + +/** + * True when a WebP RIFF container includes an ANIM chunk. + */ +export function isAnimatedWebp( data: Buffer ) { + if ( + data.length < 12 || + data.subarray( 0, 4 ).toString() !== 'RIFF' || + data.subarray( 8, 12 ).toString() !== 'WEBP' + ) { + return false + } + + let offset = 12 + while ( offset + 8 <= data.length ) { + const fourcc = data.subarray( offset, offset + 4 ).toString() + const chunkLength = data.readUInt32LE( offset + 4 ) + if ( fourcc === 'ANIM' ) { + return true + } + offset += 8 + chunkLength + ( chunkLength % 2 ) + } + + return false +} + /** * Click a control that opens a new tab/window and return that document's byte size. */ diff --git a/e2e/tests/animated-webp.spec.ts b/e2e/tests/animated-webp.spec.ts new file mode 100644 index 0000000..23fdb55 --- /dev/null +++ b/e2e/tests/animated-webp.spec.ts @@ -0,0 +1,176 @@ +import fs from 'fs' + +import { + test, + expect, + SAMPLE_STILL_GIF, + SAMPLE_ANIMATED_GIF, + SAMPLE_ANIMATED_WEBP, + SAMPLE_ANIMATED_TRANSPARENT_GIF, + saveCimoOptions, + uploadSampleViaMediaNew, + getMediaFileByteLength, + fetchUrlBytes, + isAnimatedWebp, + reloadCimoRuntime, +} from '../test-utils' + +test.describe.configure( { timeout: 180_000 } ) + +async function sampleImageAlpha( + page: import( '@playwright/test' ).Page, + url: string, + x: number, + y: number +) { + return await page.evaluate( async ( { src, sampleX, sampleY } ) => { + const image = new Image() + image.crossOrigin = 'anonymous' + image.src = src + await image.decode() + const canvas = document.createElement( 'canvas' ) + canvas.width = image.naturalWidth + canvas.height = image.naturalHeight + const context = canvas.getContext( '2d' ) + if ( ! context ) { + throw new Error( 'Canvas 2D is unavailable' ) + } + context.clearRect( 0, 0, canvas.width, canvas.height ) + context.drawImage( image, 0, 0 ) + return context.getImageData( sampleX, sampleY, 1, 1 ).data[ 3 ] + }, { src: url, sampleX: x, sampleY: y } ) +} + +test.describe( 'Animated WebP', () => { + test.beforeEach( async ( { requestUtils } ) => { + await requestUtils.deleteAllMedia() + await saveCimoOptions( requestUtils, { + webp_quality: 80, + max_image_dimension: 0, + disable_wp_scaling: 1, + disable_thumbnail_generation: 1, + smart_optimization: 0, + } ) + } ) + + test.afterEach( async ( { requestUtils } ) => { + await requestUtils.deleteAllMedia() + await saveCimoOptions( requestUtils, { + webp_quality: 80, + max_image_dimension: 0, + disable_thumbnail_generation: 0, + } ) + } ) + + test( 'converts an animated GIF to animated WebP', async ( { + page, + requestUtils, + } ) => { + await reloadCimoRuntime( page ) + + const media = await uploadSampleViaMediaNew( + page, + requestUtils, + SAMPLE_ANIMATED_GIF, + 'image/gif' + ) + + expect( media.mime_type ).toBe( 'image/webp' ) + expect( media.source_url ).toMatch( /\.webp(\?|$)/i ) + expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true ) + expect( await getMediaFileByteLength( page, requestUtils, media.id ) ).toBeGreaterThan( 0 ) + } ) + + test( 'converts a still GIF to still WebP', async ( { + page, + requestUtils, + } ) => { + await reloadCimoRuntime( page ) + + const media = await uploadSampleViaMediaNew( + page, + requestUtils, + SAMPLE_STILL_GIF, + 'image/gif' + ) + + expect( media.mime_type ).toBe( 'image/webp' ) + expect( media.source_url ).toMatch( /\.webp(\?|$)/i ) + expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( false ) + } ) + + test( 'optimizes an animated WebP and keeps it animated', async ( { + page, + requestUtils, + } ) => { + await reloadCimoRuntime( page ) + const originalSize = fs.statSync( SAMPLE_ANIMATED_WEBP ).size + + const media = await uploadSampleViaMediaNew( + page, + requestUtils, + SAMPLE_ANIMATED_WEBP, + 'image/webp', + { + expectedMime: 'image/webp', + urlPattern: /\.webp(\?|$)/i, + } + ) + + expect( media.mime_type ).toBe( 'image/webp' ) + expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true ) + + const uploadedSize = await getMediaFileByteLength( page, requestUtils, media.id ) + expect( uploadedSize ).toBeGreaterThan( 0 ) + expect( uploadedSize ).toBeLessThanOrEqual( originalSize * 1.25 ) + } ) + + test( 'converts a transparent animated GIF to animated WebP and keeps the background transparent', async ( { + page, + requestUtils, + } ) => { + await reloadCimoRuntime( page ) + + const media = await uploadSampleViaMediaNew( + page, + requestUtils, + SAMPLE_ANIMATED_TRANSPARENT_GIF, + 'image/gif' + ) + + expect( media.mime_type ).toBe( 'image/webp' ) + expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true ) + + const width = media.media_details?.width ?? 0 + const height = media.media_details?.height ?? 0 + expect( width ).toBeGreaterThan( 1 ) + expect( height ).toBeGreaterThan( 1 ) + expect( await sampleImageAlpha( page, media.source_url, 0, 0 ) ).toBe( 0 ) + expect( await sampleImageAlpha( page, media.source_url, width - 1, 0 ) ).toBe( 0 ) + expect( await sampleImageAlpha( page, media.source_url, 80, 80 ) ).toBeGreaterThan( 200 ) + } ) + + test( 'resizes an animated GIF without flattening it', async ( { + page, + requestUtils, + } ) => { + await saveCimoOptions( requestUtils, { + max_image_dimension: 80, + } ) + await reloadCimoRuntime( page ) + + const media = await uploadSampleViaMediaNew( + page, + requestUtils, + SAMPLE_ANIMATED_GIF, + 'image/gif' + ) + + expect( media.mime_type ).toBe( 'image/webp' ) + expect( Math.max( + media.media_details?.width ?? 0, + media.media_details?.height ?? 0 + ) ).toBeLessThanOrEqual( 80 ) + expect( isAnimatedWebp( await fetchUrlBytes( page, media.source_url ) ) ).toBe( true ) + } ) +} ) diff --git a/e2e/tests/quality-and-webp.spec.ts b/e2e/tests/quality-and-webp.spec.ts index 523424e..23f4f05 100644 --- a/e2e/tests/quality-and-webp.spec.ts +++ b/e2e/tests/quality-and-webp.spec.ts @@ -5,12 +5,41 @@ import { expect, SAMPLE_LARGE_JPG, SAMPLE_LARGE_WEBP, + expectNewMediaMime, + getMaxMediaId, saveCimoOptions, uploadSampleViaMediaNew, getMediaFileByteLength, reloadCimoRuntime, + waitForCimoReady, } from '../test-utils' +const SMALL_ANIMATED_GIF = Buffer.from( + 'R0lGODlhAQABAIAAAP8AAAD/ACH/C05FVFNDQVBFMi4wAwEAAAAh+QQACgAAACwAAAAAAQABAAACAkQBACH5BAAKAAAALAAAAAABAAEAAAICTAEAOw==', + 'base64' +) + +async function uploadBufferViaMediaNew( + page: import( '@playwright/test' ).Page, + requestUtils: import( '@wordpress/e2e-test-utils-playwright' ).RequestUtils, + file: { name: string, mimeType: string, buffer: Buffer }, + expectedMime = 'image/webp' +) { + await page.goto( '/wp-admin/media-new.php' ) + await waitForCimoReady( page ) + const afterId = await getMaxMediaId( requestUtils ) + const fileInput = page.locator( + '.media-upload-form input[type="file"], #async-upload, input[name="async-upload"]' + ).first() + await expect( fileInput ).toBeAttached( { timeout: 15_000 } ) + await fileInput.setInputFiles( file ) + + return await expectNewMediaMime( requestUtils, afterId, expectedMime, { + timeout: 120_000, + urlPattern: new RegExp( `\\.${ expectedMime === 'image/webp' ? 'webp' : 'gif' }(\\?|$)`, 'i' ), + } ) +} + test.describe.configure( { timeout: 180_000 } ) test.describe( 'WebP quality and already-WebP uploads', () => { @@ -138,4 +167,23 @@ test.describe( 'WebP quality and already-WebP uploads', () => { expect( media.media_details?.width ?? 0 ).toBeGreaterThan( 800 ) expect( await getMediaFileByteLength( page, requestUtils, media.id ) ).toBe( originalSize ) } ) + + test( 'keeps an animated GIF when its WebP conversion would be larger', async ( { + page, + requestUtils, + } ) => { + await saveCimoOptions( requestUtils, { + webp_quality: 100, + smart_optimization: 0, + disable_thumbnail_generation: 1, + } ) + await reloadCimoRuntime( page ) + + const retainedGif = await uploadBufferViaMediaNew( page, requestUtils, { + name: 'animated.gif', + mimeType: 'image/gif', + buffer: SMALL_ANIMATED_GIF, + }, 'image/gif' ) + expect( retainedGif.source_url ).toMatch( /\.gif(\?|$)/i ) + } ) } ) diff --git a/package-lock.json b/package-lock.json index 059ba89..635d3b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,11 +12,13 @@ "@mediabunny/mp3-encoder": "^1.25.0", "browser-image-compression": "^2.0.2", "classnames": "^2.5.1", + "gifuct-js": "2.1.2", "heic2any": "^0.0.4", "mediabunny": "^1.25.0", "mime": "^4.1.0", "resemblejs": "^5.0.0", - "svgo": "^4.0.0" + "svgo": "^4.0.0", + "wasm-webp": "0.1.0" }, "devDependencies": { "@playwright/test": "^1.61.1", @@ -31,7 +33,8 @@ "eslint": "^8.57.0", "eslint-import-resolver-typescript": "^4.4.4", "eslint-plugin-compat": "^6.0.2", - "prettier": "^3.3.3" + "prettier": "^3.3.3", + "typescript": "5.3.3" }, "engines": { "node": ">=18" @@ -9316,6 +9319,14 @@ "integrity": "sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==", "dev": true }, + "node_modules/devtools-protocol": { + "version": "0.0.1687809", + "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1687809.tgz", + "integrity": "sha512-t7kSb+UKYdyxcmqy98U2P86ne6rCve3DIJ4+lp54i/hhClgFuG35JrLj23rO14Jw3sWDsp7i+22QhNU0+/qk8g==", + "dev": true, + "license": "BSD-3-Clause", + "peer": true + }, "node_modules/diff-sequences": { "version": "29.6.3", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-29.6.3.tgz", @@ -11750,6 +11761,15 @@ "node": ">= 14" } }, + "node_modules/gifuct-js": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/gifuct-js/-/gifuct-js-2.1.2.tgz", + "integrity": "sha512-rI2asw77u0mGgwhV3qA+OEgYqaDn5UNqgs+Bx0FGwSpuqfYn+Ir6RQY5ENNQ8SbIiG/m5gVa7CD5RriO4f4Lsg==", + "license": "MIT", + "dependencies": { + "js-binary-schema-parser": "^2.0.3" + } + }, "node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -14030,6 +14050,12 @@ "integrity": "sha512-WZzeDOEtTOBK4Mdsar0IqEU5sMr3vSV2RqkAIzUEV2BHnUfKGyswWFPFwK5EeDo93K3FohSHbLAjj0s1Wzd+dg==", "dev": true }, + "node_modules/js-binary-schema-parser": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/js-binary-schema-parser/-/js-binary-schema-parser-2.0.3.tgz", + "integrity": "sha512-xezGJmOb4lk/M1ZZLTR/jaBHQ4gG/lqQnJqdIv4721DMggsa1bDVlHXNeHYogaIEHD9vCRv0fcL4hMA+Coarkg==", + "license": "MIT" + }, "node_modules/js-library-detector": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/js-library-detector/-/js-library-detector-6.7.0.tgz", @@ -20764,6 +20790,20 @@ "is-typedarray": "^1.0.0" } }, + "node_modules/typescript": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.3.3.tgz", + "integrity": "sha512-pXWcraxM0uxAS+tN0AG/BF2TyqmHO014Z070UsJ+pFvYuRSq8KH8DmWpnbXe0pEPDHXZV3FcAbJkijJ5oNEnWw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/uc.micro": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz", @@ -21166,6 +21206,12 @@ "dev": true, "license": "Apache-2.0" }, + "node_modules/wasm-webp": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/wasm-webp/-/wasm-webp-0.1.0.tgz", + "integrity": "sha512-Z1Sop+mLKogzDK3vJzSPfSxRF1yj/zbmn+XJuU9+7fvarxcuGhGkeLBqJr5H8LdgWuAwEKuZelCzx7ujztfhVQ==", + "license": "MIT" + }, "node_modules/watchpack": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/watchpack/-/watchpack-2.5.1.tgz", diff --git a/package.json b/package.json index 00dde80..09071ca 100644 --- a/package.json +++ b/package.json @@ -48,16 +48,19 @@ "eslint": "^8.57.0", "eslint-import-resolver-typescript": "^4.4.4", "eslint-plugin-compat": "^6.0.2", - "prettier": "^3.3.3" + "prettier": "^3.3.3", + "typescript": "5.3.3" }, "dependencies": { "@mediabunny/mp3-encoder": "^1.25.0", "browser-image-compression": "^2.0.2", "classnames": "^2.5.1", + "gifuct-js": "2.1.2", "heic2any": "^0.0.4", "mediabunny": "^1.25.0", "mime": "^4.1.0", "resemblejs": "^5.0.0", - "svgo": "^4.0.0" + "svgo": "^4.0.0", + "wasm-webp": "0.1.0" } } diff --git a/src/shared/converters/animated-image.js b/src/shared/converters/animated-image.js new file mode 100644 index 0000000..b6282bf --- /dev/null +++ b/src/shared/converters/animated-image.js @@ -0,0 +1,318 @@ +let webpModulePromise = null + +/** + * Return the output dimensions after applying Cimo's maximum-dimension rule. + * + * @param {number} width - Source width. + * @param {number} height - Source height. + * @param {number} maxDimension - Largest permitted output side. + * @return {{ width: number, height: number }} Output dimensions. + */ +function getOutputDimensions( width, height, maxDimension ) { + if ( ! maxDimension || Math.max( width, height ) <= maxDimension ) { + return { width, height } + } + + // Scale both sides by the same factor so animation frames retain their aspect ratio. + const scale = maxDimension / Math.max( width, height ) + return { + width: Math.round( width * scale ), + height: Math.round( height * scale ), + } +} + +/** + * Draw GIF patches onto a full-size canvas while honoring the disposal method. + * + * @param {Array} frames - Frames returned by gifuct-js. + * @param {number} width - GIF canvas width. + * @param {number} height - GIF canvas height. + * @return {Array<{data: Uint8Array, duration: number}>} Fully composited frames. + */ +function composeGifFrames( frames, width, height ) { + const canvas = document.createElement( 'canvas' ) + const patchCanvas = document.createElement( 'canvas' ) + canvas.width = width + canvas.height = height + const context = canvas.getContext( '2d' ) + const patchContext = patchCanvas.getContext( '2d' ) + + if ( ! context || ! patchContext ) { + throw new Error( 'Canvas is unavailable for GIF conversion' ) + } + + return frames.map( frame => { + const { + left, + top, + width: frameWidth, + height: frameHeight, + } = frame.dims + const previousCanvas = + frame.disposalType === 3 + ? context.getImageData( 0, 0, width, height ) + : null + + // gifuct-js returns a changed-area patch, not a complete image frame. + // Drawing it onto the accumulated canvas reconstructs what the browser displays. + patchCanvas.width = frameWidth + patchCanvas.height = frameHeight + patchContext.putImageData( + new ImageData( + new Uint8ClampedArray( frame.patch ), + frameWidth, + frameHeight, + ), + 0, + 0, + ) + context.drawImage( patchCanvas, left, top ) + + // Animated WebP needs a complete RGBA canvas for every encoded frame. + const outputFrame = { + data: new Uint8Array( + context.getImageData( 0, 0, width, height ).data, + ), + duration: frame.delay || 100, + } + + // Prepare the canvas for the next GIF patch according to the disposal rule. + if ( frame.disposalType === 2 ) { + context.clearRect( left, top, frameWidth, frameHeight ) + } else if ( previousCanvas ) { + context.putImageData( previousCanvas, 0, 0 ) + } + + return outputFrame + } ) +} + +/** + * Identify the animation chunks in a valid WebP RIFF container. + * + * @param {Uint8Array} data - WebP bytes. + * @return {boolean} Whether the file is animated. + */ +function isAnimatedWebp( data ) { + if ( + data.length < 12 || + String.fromCharCode( ...data.slice( 0, 4 ) ) !== 'RIFF' || + String.fromCharCode( ...data.slice( 8, 12 ) ) !== 'WEBP' + ) { + return false + } + + const view = new DataView( data.buffer, data.byteOffset, data.byteLength ) + for ( let offset = 12; offset + 8 <= data.length; ) { + const chunkLength = view.getUint32( offset + 4, true ) + // ANIM is the WebP container chunk that enables animation metadata and frames. + if ( String.fromCharCode( ...data.slice( offset, offset + 4 ) ) === 'ANIM' ) { + return true + } + // RIFF chunks are padded to an even byte boundary. + offset += 8 + chunkLength + ( chunkLength % 2 ) + } + + return false +} + +/** + * Decode an animated GIF or WebP into full-canvas RGBA frames. + * + * @param {File|Blob} file - Source image. + * @return {Promise}>} Animation data. + */ +async function decodeAnimation( file ) { + // The GIF parser and the WebP RIFF check both operate on the original bytes. + const data = new Uint8Array( await file.arrayBuffer() ) + + if ( file.type === 'image/gif' ) { + // Load the GIF parser only for GIF uploads. + const { parseGIF, decompressFrames } = await import( + /* webpackChunkName: "animated-webp-codec" */ 'gifuct-js' + ) + const gif = parseGIF( data.buffer ) + const frames = decompressFrames( gif, true ) + // A single GIF frame follows the regular canvas conversion path instead. + if ( frames.length < 2 ) { + return null + } + + return { + width: gif.lsd.width, + height: gif.lsd.height, + frames: composeGifFrames( frames, gif.lsd.width, gif.lsd.height ), + } + } + + // Only animated WebP needs frame-aware decoding. Still WebP remains on the canvas path. + if ( file.type !== 'image/webp' || ! isAnimatedWebp( data ) ) { + return null + } + + const webpModule = await getWebpModule() + // The codec reconstructs complete canvases and exposes the display duration per frame. + const frames = webpModule.decodeAnimation( data, true ) + if ( ! frames || frames.length < 2 ) { + throw new Error( 'Failed to decode animated WebP' ) + } + + return { + width: frames[ 0 ].width, + height: frames[ 0 ].height, + frames: frames.map( frame => ( { + data: new Uint8Array( frame.data ), + duration: frame.duration || 100, + } ) ), + } +} + +/** + * Resize full-canvas animation frames when Cimo has a maximum dimension. + * + * @param {{width: number, height: number, frames: Array<{data: Uint8Array, duration: number}>}} animation - Decoded frames. + * @param {number} maxDimension - Largest permitted output side. + * @return {{width: number, height: number, frames: Array<{data: Uint8Array, duration: number}>}} Resized animation. + */ +function resizeAnimation( animation, maxDimension ) { + const dimensions = getOutputDimensions( + animation.width, + animation.height, + maxDimension, + ) + if ( + dimensions.width === animation.width && + dimensions.height === animation.height + ) { + return animation + } + + // Resize complete decoded frames rather than GIF patches, preserving the displayed image. + const sourceCanvas = document.createElement( 'canvas' ) + const outputCanvas = document.createElement( 'canvas' ) + sourceCanvas.width = animation.width + sourceCanvas.height = animation.height + outputCanvas.width = dimensions.width + outputCanvas.height = dimensions.height + const sourceContext = sourceCanvas.getContext( '2d' ) + const outputContext = outputCanvas.getContext( '2d' ) + if ( ! sourceContext || ! outputContext ) { + throw new Error( 'Canvas is unavailable for animation resizing' ) + } + + return { + ...animation, + ...dimensions, + frames: animation.frames.map( frame => { + // Load the source RGBA pixels, resize them, then read back WebP-ready RGBA data. + sourceContext.putImageData( + new ImageData( + new Uint8ClampedArray( frame.data ), + animation.width, + animation.height, + ), + 0, + 0, + ) + outputContext.clearRect( 0, 0, dimensions.width, dimensions.height ) + outputContext.drawImage( + sourceCanvas, + 0, + 0, + dimensions.width, + dimensions.height, + ) + return { + ...frame, + data: new Uint8Array( + outputContext.getImageData( + 0, + 0, + dimensions.width, + dimensions.height, + ).data, + ), + } + } ), + } +} + +/** + * Convert an animated GIF or WebP to animated WebP. + * Returns null for a non-animated source so the normal image path stays fast. + * + * @param {File|Blob} file - Source image. + * @param {number} quality - WebP quality, as 0-1 or 0-100. + * @param {number} maxDimension - Largest permitted output side. + * @return {Promise} Encoded animated WebP, or null for a still image. + */ +export async function convertAnimatedImageToWebp( file, quality, maxDimension ) { + const decodedAnimation = await decodeAnimation( file ) + // Let ImageConverter use its lighter single-image path when the source is not animated. + if ( ! decodedAnimation ) { + return null + } + + // Apply Cimo's resize setting before encoding every frame. + const animation = resizeAnimation( decodedAnimation, maxDimension ) + const webpModule = await getWebpModule() + // Settings may reach this code as a 0-1 canvas value or a 0-100 WebP value. + const normalizedQuality = Math.round( + Math.min( 100, Math.max( 0, quality <= 1 ? quality * 100 : quality ) ), + ) + const frames = new webpModule.VectorWebPAnimationFrame() + try { + animation.frames.forEach( frame => { + // Keep timing and alpha-capable RGBA pixels with their per-frame quality setting. + frames.push_back( { + duration: frame.duration, + data: frame.data, + config: { lossless: 0, quality: normalizedQuality }, + // eslint-disable-next-line camelcase -- This is the WASM encoder's required field name. + has_config: true, + } ) + } ) + // The encoder assembles the supplied full-size frames into one animated WebP container. + const data = webpModule.encodeAnimation( + animation.width, + animation.height, + true, + frames, + ) + if ( ! data ) { + throw new Error( 'Failed to encode animated WebP' ) + } + + return new Blob( [ data ], { type: 'image/webp' } ) + } finally { + // Emscripten vectors own native memory and must be explicitly released. + frames.delete() + } +} + +/** + * Load the WebP codec with the emitted WebAssembly asset URL. + * + * @return {Promise} Initialized Emscripten module. + */ +async function getWebpModule() { + if ( ! webpModulePromise ) { + // Cache the initialization so consecutive animated uploads share one codec. + webpModulePromise = Promise.all( [ + import( + /* webpackChunkName: "animated-webp-codec" */ 'wasm-webp/dist/cjs/webp-wasm.js' + ), + import( + /* webpackChunkName: "animated-webp-codec" */ 'wasm-webp/dist/cjs/webp-wasm.wasm' + ), + ] ).then( ( [ importedModule, wasmAsset ] ) => { + const createModule = importedModule.default || importedModule + const webpWasmUrl = wasmAsset.default + return createModule( { + locateFile: () => webpWasmUrl, + } ) + } ) + } + + return webpModulePromise +} diff --git a/src/shared/converters/conversion-engine.md b/src/shared/converters/conversion-engine.md index d3dfcd4..b1c4d1c 100644 --- a/src/shared/converters/conversion-engine.md +++ b/src/shared/converters/conversion-engine.md @@ -16,7 +16,7 @@ Max dimension is min(user max, WP scaling threshold) when both set. - `convert()`: canvas resize/compress; PNG path may use `browser-image-compression`; skip write if larger. - `optimize()`: if smart flag, async filter `cimo.imageConverter.optimize`; else `convert()`; may stamp `smartOptimized`. -- Animated GIF scan avoids breaking animations. +- Animated GIF and animated WebP decode to full frames, then re-encode through the animated WebP codec so upload interception never flattens them. ## Premium registration (reference) diff --git a/src/shared/converters/image-converter.js b/src/shared/converters/image-converter.js index 45d5a3f..6344206 100644 --- a/src/shared/converters/image-converter.js +++ b/src/shared/converters/image-converter.js @@ -1,4 +1,5 @@ import { Converter } from './converter-abstract' +import { convertAnimatedImageToWebp } from './animated-image' import { applyFilters, applyFiltersAsync } from '@wordpress/hooks' import { __ } from '@wordpress/i18n' @@ -32,6 +33,7 @@ class ImageConverter extends Converter { static get mimeTypes() { // Accept all images supported by most browsers for conversion. return applyFilters( 'cimo.imageConverter.mimeTypes', [ + 'image/gif', 'image/jpeg', 'image/png', 'image/webp', @@ -267,34 +269,6 @@ class ImageConverter extends Converter { } } - // Detect if the image is an animated GIF, if so just return the file unchanged - if ( file.type === 'image/gif' ) { - // Read the first few bytes to check for animation - const buffer = await file.slice( 0, 50 * 1024 ).arrayBuffer() - const bytes = new Uint8Array( buffer ) - - // Look for multiple Graphic Control Extension blocks (0x21, 0xF9, 0x04) - let gceCount = 0 - for ( let i = 0; i < bytes.length - 2; i++ ) { - if ( - bytes[ i ] === 0x21 && - bytes[ i + 1 ] === 0xF9 && - bytes[ i + 2 ] === 0x04 - ) { - gceCount++ - // If more than one GCE block, it's animated - if ( gceCount > 1 ) { - return { - file, - metadata: null, - reason: 'animated-gif', - } - } - } - } - // If not animated, continue to convert - } - // Create a fileItem object for the convertImage function const fileItem = { file } @@ -308,7 +282,16 @@ class ImageConverter extends Converter { const start = performance.now() - let convertedBlob + let convertedBlob = null + // Only GIF and WebP sources can contain animation. + const mayBeAnimated = file.type === 'image/gif' || file.type === 'image/webp' + if ( format.value === 'webp' && mayBeAnimated ) { + convertedBlob = await convertAnimatedImageToWebp( + file, + quality, + this.options?.maxDimension || 0, + ) + } if ( format.value === 'png' ) { const { default: imageCompression } = await import( /* webpackChunkName: "browser-image-compression" */ 'browser-image-compression' ) convertedBlob = await imageCompression( fileItem.file, { @@ -317,7 +300,7 @@ class ImageConverter extends Converter { maxIteration: 5, // initialQuality: quality, // Use default } ) - } else { + } else if ( ! convertedBlob ) { convertedBlob = await this.convertImage( fileItem, format.value, { quality, maxDimension: this.options?.maxDimension || 0, diff --git a/webpack.config.js b/webpack.config.js index 5cdff2a..76d759b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -20,11 +20,33 @@ module.exports = { }, resolve: { ...defaultConfig.resolve, + fallback: { + ...defaultConfig.resolve?.fallback, + fs: false, + path: false, + }, alias: { ...defaultConfig.resolve?.alias, '~cimo': require( 'path' ).resolve( __dirname, 'src' ), }, }, + module: { + ...defaultConfig.module, + rules: [ + ...defaultConfig.module.rules, + { + test: /webp-wasm\.wasm$/, + type: 'asset/resource', + generator: { + filename: 'chunks/[name]-[contenthash:8][ext]', + }, + }, + { + test: /node_modules[/\\]wasm-webp[/\\]dist[/\\]cjs[/\\].*\.js$/, + type: 'javascript/auto', + }, + ], + }, output: { ...defaultConfig.output, chunkFilename: 'chunks/[name]-[chunkhash:8].js',